0

Here is my code for some settings

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"];
        }

And here is the XAML behind it

 <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"/>

Now, the XAML works fine, as well as the ToggleSwitch. All that works. Just, when I try to actually open up my settings page (which opens just fine without the CheckBox stuff, but refuses to open with it), I get this error

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).

Has anyone seen this before? Searching Bing doesn't seem to have anything for me. Any help would be greatly appreciated!

Thanks, Patrick

EDIT: I've changed the code to this

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

And now it errors on the

settings.Add("dynamicOn", true);

And the error has also changed to

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

So, I'm starting to think that this might be an IsolatedStorage issue?

PatrickC
  • 33
  • 1
  • 5
  • Rather than guessing, get the stack trace of the exception and see exactly which line of code is causing the problem. – RobSiklos Aug 27 '13 at 14:07

3 Answers3

1

You're checking that settings contains dynamicOn, but what about changeAllPacks. Probably that setting is not defined and that's why you're getting the error.

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
0

How is 'settings' defined? If you are trying to store application settings, try using IsolatedStoragSettings.ApplicationSettings instead. I use that, and actually have helper methods in a SettingsManager class for getting and setting any values. (The Mutex code is in there, since my settings are accessed by both the foreground and a background task, and that keeps any conflicts from occurring.)

The default value also makes your code simpler, since you don't have to check if the value exists first.

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();
}
Jeffrey Harmon
  • 2,268
  • 23
  • 18
  • Isolated storage access can be flaky - you would do well to put that code in a `try` block, and put the `mutex.ReleaseMutex();` in a `finally` – RobSiklos Aug 27 '13 at 14:06
0

Not sure exactly what happened.. But somehow the issue seemed to resolve itself.. Thanks for all the help anyway though!

PatrickC
  • 33
  • 1
  • 5