在桌面应用程序或Web项目项目中,有App.configs和Web.configs文件来存储设置。这些设置是在开发时间(或稍后)中设置的,但如果发生这种情况,则始终是一次操作。
在Windows Phone 8.1 XAML中,没有任何App.config文件,因此开发人员可以使用Windows.Storage.ApplicationData.Current.LocalSettings
。尼斯。
如何设置这些设置第一次(这意味着在第一次运行应用程序时,所以我以后只能阅读它们,有时会更新现有值)?当然,每当我运行应用程序时,我都可以设置设置,但这是浪费时间。如何在第一次在应用程序中设置LocalSettings?我看到了这个解决方案Is there a "first run" flag in WP7,但我不这么认为,这是唯一的可能性。
答案 0 :(得分:11)
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
// Create a simple setting
localSettings.Values["exampleSetting"] = "Hello Windows";
// Read data from a simple setting
Object value = localSettings.Values["exampleSetting"];
if (value == null)
{
// No data
}
else
{
// Access data in value
}
// Delete a simple setting
localSettings.Values.Remove("exampleSetting");
答案 1 :(得分:4)
我写了代码:
public void Initialize()
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!localSettings.Values.ContainsKey(FirstRunSettingName))
{
localSettings.Values.Add(FirstRunSettingName, false);
}
localSettings.Values.Add(SettingNames.DataFilename, "todo.data.xml");
}
public bool IsFirstRun()
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey(FirstRunSettingName))
{
return (bool)localSettings.Values[FirstRunSettingName];
}
else
{
return true;
}
}
在App.xaml.cs文件中:
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
var storageService = Container.Get<ISettingsService>();
if (storageService.IsFirstRun())
{
storageService.Initialize();
}
}
我不确定这是第一次设置设置的正确方法,但这是一些灵魂。