0

这是我的一些设置代码

if (settings.Contains("dynamicOn"))
        {
            // Commenting out because we're trying a different way
            // DynamicCheck.IsChecked = (bool)settings["dynamicOn"];
            dynamicToggle.IsChecked = (bool)settings["dynamicOn"];
            AllPacks.IsChecked = (bool)settings["changeAllPacks"];
        }

这是它背后的 XAML

 <toolkit:ToggleSwitch x:Name="dynamicToggle" Header="Dynamically Update Lockscreen?" Margin="0,112,0,-112" SwitchForeground="White" Checked="DynamicCheck_Checked_1" Unchecked="DynamicCheck_Unchecked">
                        <toolkit:ToggleSwitch.HeaderTemplate>
                            <DataTemplate>
                                <ContentControl Content="{Binding}"/>
                            </DataTemplate>
                        </toolkit:ToggleSwitch.HeaderTemplate>
                    </toolkit:ToggleSwitch>
                    <CheckBox x:Name="AllPacks" Content="All Packs" Checked="AllPacks_Checked" Unchecked="AllPacks_Unchecked" Margin="50,174,0,0" Visibility="Collapsed"/>

现在,XAML 和 ToggleSwitch 都可以正常工作。所有这些都有效。只是,当我尝试实际打开我的设置页面时(没有 CheckBox 的东西打开就好了,但拒绝用它打开),我收到了这个错误

A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll The program '[3048] TaskHost.exe' has exited with code -1 (0xffffffff).

有没有人见过这个?搜索 Bing 似乎对我没有任何帮助。任何帮助将不胜感激!

谢谢,帕特里克

编辑:我已将代码更改为此

if (settings.Contains("dynamicOn"))
        {
            // Commenting out because we're trying a different way
            // DynamicCheck.IsChecked = (bool)settings["dynamicOn"];
            AllPacks.IsChecked = (bool)settings["changeAllPacks"];
            dynamicToggle.IsChecked = (bool)settings["dynamicOn"];
        }
        else
        {
            // Commenting out because we're trying a different way
            // DynamicCheck.IsChecked = true;
            // isEnabledText.Visibility = Visibility.Visible;
            // settings.Add("dynamicOn", true);
            dynamicToggle.IsChecked = true;
            AllPacks.IsChecked = true;
            AllPacks.Visibility = Visibility.Visible;
            settings.Add("changeAllPacks", true);
            settings.Add("dynamicOn", true);
        }

现在它在

settings.Add("dynamicOn", true);

并且错误也更改为

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll

所以,我开始认为这可能是一个独立存储问题?

4

3 回答 3

1

您正在检查settingscontains dynamicOn,但是changeAllPacks. 可能该设置未定义,这就是您收到错误的原因。

于 2013-08-26T19:25:48.460 回答
0

不知道到底发生了什么.. 但不知何故,问题似乎自行解决了.. 无论如何感谢所有帮助!

于 2013-08-27T16:24:07.993 回答
0

“设置”是如何定义的?如果您尝试存储应用程序设置,请尝试改用 IsolatedStoragSettings.ApplicationSettings。我使用它,并且实际上在 SettingsManager 类中有辅助方法来获取和设置任何值。(互斥代码就在那里,因为前台和后台任务都可以访问我的设置,这样可以防止发生任何冲突。)

默认值还使您的代码更简单,因为您不必先检查该值是否存在。

public static T GetSetting<T>(string settingKey, T defaultValue)
{
    Mutex mutex = new Mutex(false, "MyAppSettingsLock");
    mutex.WaitOne();

    T result;
    if (IsolatedStorageSettings.ApplicationSettings.Contains(settingKey))
        result = (T)IsolatedStorageSettings.ApplicationSettings[settingKey];
    else
        result = defaultValue;

    mutex.ReleaseMutex();

    return result;
}

public static void SetSetting<T>(string settingKey, T value)
{
    Mutex mutex = new Mutex(false, "MyAppSettingsLock");
    mutex.WaitOne();

    IsolatedStorageSettings.ApplicationSettings[settingKey] = value;
    IsolatedStorageSettings.ApplicationSettings.Save();

    mutex.ReleaseMutex();
}
于 2013-08-27T01:53:44.853 回答