绑定到ApplicationData RoamingSettings值崩溃应用程序

时间:2013-12-30 15:43:59

标签: c# xaml windows-store-apps

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

在一些fiddeling之后,我收到以下错误:

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中的设置。 RoamingSettings是否正确实现了IObservableMap,或者是否存在一些处置?我的代码基于BlankPage模板。

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>

1 个答案:

答案 0 :(得分:0)

您应该注册DataChanged事件并添加一个事件处理程序,它将使用TextBox的新值更新漫游设置“Hello”。这将确保您的应用可以正确处理对漫游设置的修改。

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