40

私はUniversal Windows Platformアプリケーションを開発していますが、Visual Studioには設定テンプレートはありません。UWPアプリケーションに設定クラスを追加する

LocalSettingsまたはRoamingSettingsに自分の設定を保存する、簡単で強く型付けされ、観測可能なクラスを実装するにはどうすればよいですか?

+13

http://meta.stackoverflow.com/questions/317127/hidden-advertisement-as-spam –

答えて

51
  1. ObservableSettingsから継承する新しいクラスを作成します。
  2. LocalSettingsまたはRoamingSettingsに設定を保存するかどうかを示す基本クラスコンストラクタを呼び出します。
  3. 設定ゲッターで、セッターでを取得し、基本クラスのメンバーを呼び出して、すべてのプロパティを追加します。プロパティの名前を渡す必要はありませんnameof()オペレータ!
  4. オプションで、プロパティを修飾するデフォルト値をDefaultSettingValue属性で設定できます。ここで

設定クラスの例:あなたのapp.xamlにインスタンスを追加する方法

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

    } 
} 

、ここで:

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

アクセスとMVVMの形で値を変更:

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

すべてが適切に保存されますリポジトリ。ここで

あなたはDefaultSettingValueObservableSettingsの実装を持っている:

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

あなたは、私がGitHubで作成したリポジトリからの機能の例と解決策をダウンロードすることができます。

+17

ニース自己回答!ご協力いただきありがとうございます。あなた自身のブログやプロジェクト(またはあなたの会社など)にリンクするときには、あなたのプロジェクトとの関係を明記する小さな免責事項(回答の最上部またはリンクの近く)を追加することをお勧めします。あなたの客観性に影響を与えるかもしれません。 –

関連する問題