LocalSettings 和 IsolatedStorageSettings 之间的区别

Difference between LocalSettings and IsolatedStorageSettings

我正在构建一个 Windows Phone 8.1 Silverlight 应用程序。我可以使用以下两个注册表:

Windows.Storage.ApplicationData.Current.LocalSettings;

IsolatedStorageSettings.ApplicationSettings;
  1. 这两者有什么区别?
  2. 哪个更好?

Windows.Storage.ApplicationData.Current.LocalSettingsIsolatedStorageSettings.ApplicationSettings 的区别在于第一个是较新的统一 Windows Store App API 而后者来自 "old" Silverlight API.

新的并不总是更好,但我个人认为您应该选择这里的 modern version。两者都适用于 Silverlight,但如果您必须将代码迁移到 WinRT,您将节省一些时间,因为 IsolatedStorageSettings API 在 WinRT 下不工作。

使用这两种设置有很大的不同:

  • IsolatedStorageSettingsDictionary 一样工作,它被序列化并保存到 IsolatedStorageFile:

    IsolatedStorageSettings provide a convenient way to store user specific data as key-value pairs in a local IsolatedStorageFile.

    另请注意 IsolatedStorageSettings 必须 保存 - IsolatedStorageSettings.Save。保存后,您会在应用程序的独立存储中找到一个文件 __ApplicationSettings

  • ApplicationData.LocalSettings is an ApplicationDataContainer. Once you add there a value, it's automatically saved. It's model is conceptually equivalent to the Windows registry.

所以它们是完全不同的东西,如果您将密钥添加到上述 设置 之一,那么它不会自动出现在第二个。考虑两个按钮:

const string firstKey = "firstKey";
const string secondKey = "secondKey";
IsolatedStorageSettings isoSetting = IsolatedStorageSettings.ApplicationSettings;
ApplicationDataContainer localSetting = ApplicationData.Current.LocalSettings;

private void Button_Click(object sender, RoutedEventArgs e)
{
    isoSetting.Add(firstKey, true);
    localSetting.Values[secondKey] = false;
    //isoSetting.Save(); // IsolatedStorageSettings have to be saved

    Debug.WriteLine("Is first key in LocalSettings: {0}", localSetting.Values.ContainsKey(firstKey));
    Debug.WriteLine("Is first key in ApplicationSettings: {0}", isoSetting.Contains(firstKey));
    Debug.WriteLine("Is second key in LocalSettings: {0}", localSetting.Values.ContainsKey(secondKey));
    Debug.WriteLine("Is second key in ApplicationSettings: {0}", isoSetting.Contains(secondKey));
}

private void Button_Click2(object sender, RoutedEventArgs e)
{
    // run this button after app restart without clicking first button
    // and saving IsoSettings
    Debug.WriteLine("Is first key in LocalSettings: {0}", localSetting.Values.ContainsKey(firstKey));
    Debug.WriteLine("Is first key in ApplicationSettings: {0}", isoSetting.Contains(firstKey));
    Debug.WriteLine("Is second key in LocalSettings: {0}", localSetting.Values.ContainsKey(secondKey));
    Debug.WriteLine("Is second key in ApplicationSettings: {0}", isoSetting.Contains(secondKey));
}

如果我正在编写一个新的应用程序,那么我会使用新的 ApplicationData.LocalSettings API - 它更新并且更容易移植这样的将来作为 RunTime 的应用程序,因为 WP8.1 RT 不支持 IsolatedStorageSettings.