绑定到应用程序数据漫游设置值使应用程序崩溃



我尝试直接绑定到应用程序数据漫游设置容器的值属性,但应用程序崩溃。我在文本框中键入的值将被保存并加载,但在值通过绑定更改后,应用程序会立即崩溃。我的问题是你应该直接绑定到值还是使用LoadState/SaveState?

经过一番摆弄,我得到以下错误:

System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=mscorlib
StackTrace:
   at System.Runtime.InteropServices.WindowsRuntime.IMap`2.Insert(K key, V value)
   at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Insert[K,V](IMap`2 _this, K key, V value)
   at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Indexer_Set[K,V](K key, V value)
   at App10.MainPage.<ChangeValue>d__0.MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass2`1.<OutputAsyncCausalityEvents>b__0()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
   at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass1.<OutputWaitEtwEvents>b__0()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c__DisplayClass4.<GetActionLogDelegate>b__3()
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<.cctor>b__6(Object state)
   at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
   at System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(Object thisObj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.WinRTSynchronizationContext.Invoker.Invoke()
InnerException: 

应用退出时

The program '[7124] App10.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

我会使绑定到 XAML 中的设置变得非常容易。漫游设置是否正确实现了 IObservableMap,或者是否发生了一些处置?我的代码基于空白页模板。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace App10
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public IPropertySet Settings { get; set; }
        public MainPage()
        {
            Settings = ApplicationData.Current.RoamingSettings.CreateContainer("MySettings", ApplicationDataCreateDisposition.Always).Values;
            if (!Settings.ContainsKey("Hello"))
            {
                Settings.Add("Hello", "StartingValue");
            }
            this.InitializeComponent();
        }
    }
}

还有我的 XAML 文件。

<Page
    x:Class="App10.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App10"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    DataContext="{Binding Settings, RelativeSource={RelativeSource Self}}">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBox Text="{Binding Hello,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="310,249,620,491"/>
    </Grid>
</Page>

应注册 DataChanged 事件并添加一个事件处理程序,该处理程序将使用文本框的新值更新漫游设置"Hello"。这将确保你的应用可以正确处理对漫游设置的修改。

有关示例,请参阅此 msdn 链接。

相关内容

最新更新