Windows Phone- How to set LocalSettings first time?

浪子不回头ぞ 提交于 2019-12-30 06:58:09

问题


In Desktop Application or Web Projects projects there were App.configs and Web.configs files to store settings. These settings were set in development time (or whenever later) but if this occures, it was ALWAYS once action.

In Windows Phone 8.1 XAML there isn't any App.config file, so developers are able to use Windows.Storage.ApplicationData.Current.LocalSettings. Nice.

How can I set these settings first time (this means on first application run ever, so I can later only read them and sometimes update existing values)? Of course I can set settings whenever I run application but this is time wasting. How do you set LocalSettings in you applications first time? I saw this solution Is there a "first run" flag in WP7 but I don't think so, that this is the only possibility.


回答1:


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");

Msdn Reference

Persistance of Data




回答2:


I have written code:

    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;
        }
    }

In the App.xaml.cs file:

    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;

        var storageService = Container.Get<ISettingsService>();

        if (storageService.IsFirstRun())
        {
            storageService.Initialize();
        }
    }

I'm not sure this is proper way to set settings first time, but it is some soultion.



来源:https://stackoverflow.com/questions/26798977/windows-phone-how-to-set-localsettings-first-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!