ここでは、MVVMパターンを使用したC#のような擬似コードの非常に単純な例を使って、これを行う方法を説明します。
まず、自分の設定を定義し、シリアル化/逆シリアル化されたモデルを定義します。私はNetDataContractSerializerを使ってこれを行うのが好きです。
[DataContract]
public sealed class Person
{
[DataMember]
public string Name {get;set;}
[DataMember]
public int Age {get;set;}
}
私のViewModelは私もICommand読み込みと設定を保存するために持っているでしょう私のViewModel
ではこの構成の現在のインスタンス
public sealed class ViewModel : DependencyObject
{
#region Person
/// <summary>
/// The <see cref="DependencyProperty"/> for <see cref="Person"/>.
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
PersonPropertyName,
typeof(Person),
typeof(ViewModel),
new UIPropertyMetadata(null));
/// <summary>
/// The name of the <see cref="Person"/> <see cref="DependencyProperty"/>.
/// </summary>
public const string PersonPropertyName = "Person";
/// <summary>
/// The Person
/// </summary>
public string Person
{
get { return (Person)GetValue(PersonProperty); }
set { SetValue(PersonProperty , value); }
}
#endregion
// snip
を保持している公共の財産を持っているでしょう。CanExecute
とExecute
の実行をViewModelに委譲するICommandの共通実装に関する多くの疑問があります。
ViewModelを使用して、コンフィグレーションモデルのパブリックプロパティにバインドするだけです。
<Window x:Class="Herp.DerpWindow" SnipXamlForBrevity="true">
<Window.DataContext>
<ViewModel xmlns="clr-namespace:Herp" />
</Window.DataContext>
<!-- and later... -->
<Label>The Person in Question:</Label>
<TextBlock Text="{Binding Person.Name}" />
<Label>Age</Label>
<TextBlock Text="{Binding Person.Age}" />
あなたはUIが自動的に新しい値で更新され、それに取って代わるような構成モデルは、ViewModelにの公共のDependencyPropertyに保持されているので。もちろん、バインディング更新のUIを通知する代わりの方法としてINotifyPropertyChangedを使用することはできますが、私はそれを簡単に保つことを好みます。
データバインディングはずっと前から存在していましたwpf/xaml - *個人的に*スタック間を移動する "設定"がもっと必要なのですが... –