System.Collections.Generic.KeyNotFoundException看似没有理由

时间:2013-08-26 19:21:15

标签: c# xaml windows-phone-8 windows-phone nokia

以下是我的一些设置代码

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

那么,我开始认为这可能是一个IsolatedStorage问题?

3 个答案:

答案 0 :(得分:1)

您正在检查settings是否包含dynamicOn,但changeAllPacks呢?可能没有定义设置,这就是你收到错误的原因。

答案 1 :(得分:0)

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

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

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();
}

答案 2 :(得分:0)

不确定究竟发生了什么......但不知怎的,这个问题似乎已经解决了...... 不过,谢谢你的帮助!