我正在构建一个 Windows Phone 8.1 Silverlight 应用程序。我可以使用以下两个注册表:
Windows.Storage.ApplicationData.Current.LocalSettings;
IsolatedStorageSettings.ApplicationSettings;
- 这两者有什么区别?
- 哪一个更好?
我正在构建一个 Windows Phone 8.1 Silverlight 应用程序。我可以使用以下两个注册表:
Windows.Storage.ApplicationData.Current.LocalSettings;
IsolatedStorageSettings.ApplicationSettings;
使用这两种设置有很大的不同:
IsolatedStorageSettings像Dictionary一样工作,它被序列化并保存到IsolatedStorageFile:
隔离存储设置提供了一种方便的方法,可以将用户特定数据作为键值对存储在本地隔离存储文件中。
另请注意,必须保存IsolatedStorageSettings - IsolatedStorageSettings.Save。保存后,您会在应用的隔离存储中找到一个文件__ApplicationSettings 。
ApplicationData.LocalSettings是一个ApplicationDataContainer。一旦你添加了一个值,它就会自动保存。它的模型在概念上等同于 Windows 注册表。
所以它们是完全不同的东西,如果您将密钥添加到上述设置之一,那么它不会在第二个自动出现。考虑两个按钮:
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。
Windows.Storage.ApplicationData.Current.LocalSettings
和之间的区别在于IsolatedStorageSettings.ApplicationSettings
,第一个是较新的统一 Windows Store App API,而后者来自“旧”Silverlight API。
新的并不总是更好,但我个人认为你应该在这里使用现代版本。两者都适用于 Silverlight,但如果您必须将代码迁移到 WinRT,您将节省一些时间,因为IsolatedStorageSettings
API 在 WinRT 下不起作用。