LocalSettings和IsolatedStorageSettings之间的区别

时间:2015-01-02 11:31:44

标签: windows-phone-8.1 isolatedstorage application-data

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

Windows.Storage.ApplicationData.Current.LocalSettings;

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

2 个答案:

答案 0 :(得分:0)

Windows.Storage.ApplicationData.Current.LocalSettingsIsolatedStorageSettings.ApplicationSettings之间的区别在于,第一个是较新的统一Windows应用商店应用API,而后者来自“旧的”Silverlight API。

新的并不总是更好,但我个人认为你应该选择modern version。两者都可以使用Silverlight,但是如果您必须将代码迁移到WinRT,那么由于IsolatedStorageSettings API在WinRT下不起作用,您将节省一些时间。

答案 1 :(得分:0)

使用这两种设置存在巨大差异:

所以它们完全不同,如果你在上面的设置中添加一个键,那么它就不会自动出现在第二个。考虑两个按钮:

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