Windows 手机 7 配置/appSettings?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3145803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:44:26  来源:igfitidea点击:

Windows phone 7 config / appSettings?

c#windowsxamlsilverlight-4.0windows-phone-7

提问by Rush Frisby

Is there a way to add a config file for WP7 apps like there is for Windows apps and web apps? I just need an easy way to save a few settings I'd rather not create my own object and have to serialize/deserialize an xml file. There doesn't seem to by any kind of item template that I can add to my project so just wondering if anyone has done this or an idea on the best way?

有没有办法像 Windows 应用程序和 Web 应用程序一样为 WP7 应用程序添加配置文件?我只需要一种简单的方法来保存一些设置我宁愿不创建自己的对象并且必须序列化/反序列化一个 xml 文件。似乎没有任何类型的项目模板可以添加到我的项目中,所以只是想知道是否有人以最佳方式完成了此操作或想法?

采纳答案by Rush Frisby

Found that you can do this using IsolatedStorageSettings.ApplicationSettings class.

发现您可以使用 IndependentStorageSettings.ApplicationSettings 类来做到这一点。

回答by Jacob

I wrote a simple wrapper around the IsolatedStorageSettings class that helps store and retrieve settings. Maybe you will find it useful.

我围绕 IsolatedStorageSettings 类编写了一个简单的包装器,用于帮助存储和检索设置。也许你会发现它很有用。

using System.IO.IsolatedStorage;

public static class AppSettings
{
    private static IsolatedStorageSettings Settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;

    public static void StoreSetting(string settingName, string value)
    {
        StoreSetting<string>(settingName, value);
    }

    public static void StoreSetting<TValue>(string settingName, TValue value)
    {
        if (!Settings.Contains(settingName))
            Settings.Add(settingName, value);
        else
            Settings[settingName] = value;

        // EDIT: if you don't call Save then WP7 will corrupt your memory!
        Settings.Save();
    }

    public static bool TryGetSetting<TValue>(string settingName, out TValue value)
    {            
        if (Settings.Contains(settingName))
        {
            value = (TValue)Settings[settingName];
            return true;
        }

        value = default(TValue);
        return false;
    }
}

回答by Geoffrey Hudik

IsolatedStorageSettings.ApplicationSettings does work though I just posted about some other options available including:

虽然我刚刚发布了一些其他可用选项,但IsolatedStorageSettings.ApplicationSettings 确实有效,包括:

  • App.config w/mobile configuration block
  • App.xaml / resource dictionary
  • T4 generated settings class
  • Build events
  • Protecting "private" settings
  • 带有移动配置块的 App.config
  • App.xaml / 资源字典
  • T4生成设置类
  • 构建事件
  • 保护“私人”设置

at http://www.geoffhudik.com/tech/2012/1/26/windows-phone-app-config-settings-thinking-outside-the-box.html

http://www.geoffhudik.com/tech/2012/1/26/windows-phone-app-config-settings-thinking-outside-the-box.html

回答by Rich Hopkins

Take a look at Northern Lights WP7 toolkit (in nuget), specifically at the PersistentVariables. If you're just going to save variables as settings, this'll work, and Northern Lights has a lot more to it as well.

查看 Northern Lights WP7 工具包(在 nuget 中),特别是 PersistentVariables。如果您只想将变量保存为设置,这将起作用,而且 Northern Lights 也有更多功能。

http://northernlights.codeplex.com/documentation

http://northernlights.codeplex.com/documentation