Windows Phone 8.1 Silverlight アプリを構築しています。次のレジストリの両方を使用できます。
Windows.Storage.ApplicationData.Current.LocalSettings;
IsolatedStorageSettings.ApplicationSettings;
- これら2つの違いは何ですか?
- どちらの方がよいですか?
Windows Phone 8.1 Silverlight アプリを構築しています。次のレジストリの両方を使用できます。
Windows.Storage.ApplicationData.Current.LocalSettings;
IsolatedStorageSettings.ApplicationSettings;
両方の設定を使用すると、大きな違いがあります。
IsolatedStorageSettingsはDictionaryのように機能し、シリアル化されてIsolatedStorageFileに保存されます。
IsolatedStorageSettings は、ユーザー固有のデータをキーと値のペアとしてローカルの IsolatedStorageFile に格納する便利な方法を提供します。
また、 IsolatedStorageSettings を保存する必要があることにも注意してください- IsolatedStorageSettings.Save。保存すると、アプリの分離ストレージにファイル__ApplicationSettingsが見つかります。
ApplicationData.LocalSettingsはApplicationDataContainerです。そこに値を追加すると、自動的に保存されます。そのモデルは、概念的には Windows レジストリと同等です。
したがって、それらはまったく別のものであり、上記のいずれかの設定にキーを追加すると、秒単位で自動的に表示されません。次の 2 つのボタンを検討してください。
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 を使用します。WP8.1 RT はIsolatedStorageSettingsをサポートしていないため、この API はより新しく、将来的にそのようなアプリを RunTime に移植するのがはるかに簡単になります。
Windows.Storage.ApplicationData.Current.LocalSettings
との違いはIsolatedStorageSettings.ApplicationSettings
、前者が新しい統合 Windows ストア アプリ API であるのに対し、後者は「古い」Silverlight API からのものであることです。
新しいものが常に良いとは限りませんが、個人的には、ここでは最新バージョンを使用する必要があると思います. どちらも Silverlight で動作しますが、コードを WinRT に移行する必要がある場合は、IsolatedStorageSettings
API が WinRT では動作しないため、時間を節約できます。