C# 将设置类添加到UWP应用程序

C# 将设置类添加到UWP应用程序,c#,settings,win-universal-app,appsettings,windows-10-universal,C#,Settings,Win Universal App,Appsettings,Windows 10 Universal,我正在开发一个通用Windows平台应用程序,但Visual Studio中没有设置模板 如何实现一个简单、强类型和可观察的类,将我的设置存储在LocalSettings或RoamingSettings中 创建一个继承自ObservateSettings的新类 调用基类构造函数,指示是否要将设置存储在LocalSettings或RoamingSettings中 添加调用基类成员的所有属性 在getter和setter中设置和获取。无需传递属性名称或使用nameof()运算符 (可选)您可以设置一

我正在开发一个通用Windows平台应用程序,但Visual Studio中没有设置模板

如何实现一个简单、强类型和可观察的类,将我的设置存储在LocalSettings或RoamingSettings中

  • 创建一个继承自ObservateSettings的新类
  • 调用基类构造函数,指示是否要将设置存储在LocalSettingsRoamingSettings
  • 添加调用基类成员的所有属性 在getter和setter中设置获取。无需传递属性名称或使用nameof()运算符
  • (可选)您可以设置一个默认值,用DefaultSettingValue属性装饰属性
  • 下面是设置类的示例:

    namespace Test
    {
        public class Settings : ObservableSettings
        {
            private static Settings settings = new Settings();
            public static Settings Default
            {
                get { return settings; }
            }
    
            public Settings()
                : base(ApplicationData.Current.LocalSettings)
            {
            }
    
            [DefaultSettingValue(Value = 115200)]
            public int Bauds
            {
                get { return Get<int>(); }
                set { Set(value); }
            }
    
            [DefaultSettingValue(Value = "Jose")]
            public string Name
            {
                get { return Get<string>(); }
                set { Set(value); }
            }
    
        }
    }
    
    名称空间测试
    {
    公共类设置:可观察设置
    {
    私有静态设置=新设置();
    公共静态设置默认值
    {
    获取{返回设置;}
    }
    公共设置()
    :base(ApplicationData.Current.LocalSettings)
    {
    }
    [默认设置值(值=115200)]
    公共整数波特
    {
    获取{return get();}
    集合{set(value);}
    }
    [默认设置值(Value=“Jose”)]
    公共字符串名
    {
    获取{return get();}
    集合{set(value);}
    }
    }
    }
    
    下面介绍如何在app.xaml中添加实例:

    <Application
        x:Class="Test.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        RequestedTheme="Light">
        <Application.Resources>
            <local:Settings x:Key="settings"/>
        </Application.Resources>
    </Application>
    

    <Page
        x:Class="Test.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        DataContext="{StaticResource settings}">
    
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock Text="Bauds"/>
            <TextBox Text="{Binding Default.Bauds, Mode=TwoWay}"/>
            <TextBlock Text="Name"/>
            <TextBox Text="{Binding Default.Name, Mode=TwoWay}"/>
        </StackPanel>
    </Page>
    
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Reflection;
    using System.ComponentModel;
    using Windows.Storage;
    
    
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class DefaultSettingValueAttribute : Attribute
    {
        public DefaultSettingValueAttribute()
        {
        }
    
        public DefaultSettingValueAttribute(object value)
        {
            Value = value;
        }
    
        public object Value { get; set; }
    }
    
    public class ObservableSettings : INotifyPropertyChanged
    {
        private readonly ApplicationDataContainer settings;
    
        public ObservableSettings(ApplicationDataContainer settings)
        {
            this.settings = settings;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected bool Set<T>(T value, [CallerMemberName] string propertyName = null)
        {
            if (settings.Values.ContainsKey(propertyName))
            {
                var currentValue = (T)settings.Values[propertyName];
                if (EqualityComparer<T>.Default.Equals(currentValue, value))
                    return false;
            }
    
            settings.Values[propertyName] = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            return true;
        }
    
        protected T Get<T>([CallerMemberName] string propertyName = null)
        {
            if (settings.Values.ContainsKey(propertyName))
                return (T)settings.Values[propertyName];
    
            var attributes = GetType().GetTypeInfo().GetDeclaredProperty(propertyName).CustomAttributes.Where(ca => ca.AttributeType == typeof(DefaultSettingValueAttribute)).ToList();
            if (attributes.Count == 1)
                return (T)attributes[0].NamedArguments[0].TypedValue.Value;
    
            return default(T);
        }