
(つまり、「私は不動産が変更されたことを警告しています」は、Googleの愛する「私はプロパティが変更されたことを通知します」を翻訳しています)変更通知に関して。 私は、人類が自転車の発明をどの程度発見したかを調べることにしました。
OnPropertyChanged(「プロパティ」)
OnPropertyChanged(()=>プロパティ)
SetProperty(参照ストレージ、値)
[発信者名]
[マジック]
さまざまなストライプのAOP
roslyn.codeplex.com/discussions/550266を提供してください 。
それでも、 nameof(Property)はすべての中で最もクールです-Sharp 6を注意深く見てください 。
ヒステリーの結果は次の作品で表現されます
habrahabr.ru/post/199378
habrahabr.ru/post/246469
habrahabr.ru/post/95211
個人的には、 OnPropertyChanged(nameof(Property))およびOnPropertyChanged(()=> Property)オプションが私に合っています。最初のオプションはより速く動作します。
しかし、ほとんどの場合、 BindableBaseのボックス版であるSetProperty(ref storage、value)を使用します 。
Scrooge2の足場が掲載されました
自転車の発明を止めましょう。
通常のINotifyPropertyChangedを使用すると、手は落ちませんが、手は落ちません。
完全にサポートしていますが...
さて、ファイルとハンマー。
私はきれいなコードです! ロギング、例外のキャッチ、アクセス権のチェック、あらゆる種類の通知など。 -コードを吸う: habrahabr.ru/post/88722などのProxyテンプレートの古代の知識は、コードをクリアします。
私は人生を複雑にし、インターフェイスではなくクラスを使用します。
public class Data { public virtual int Value { get; set; } public virtual string Source { get; set; } }
通知、個人的なものはありません
class BindableObject : BindableBase { public new bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) { return base.SetProperty<T>(ref storage, value, propertyName); } }
そして、すべての汚い仕事を引き受けた副本人
public class ProxyData : Data, INotifyPropertyChanged { Data _target; BindableObject _notifier; public ProxyData(Data target) { _target = target; _notifier = new BindableObject(); } public override int Value { set { int newValue = 0; if (_notifier.SetProperty(ref newValue, value)) base.Value = newValue; } } public override string Source { set { string newSource = null; if (_notifier.SetProperty(ref newSource, value)) base.Source = newSource; } } public event PropertyChangedEventHandler PropertyChanged { add { _notifier.PropertyChanged += value; } remove { _notifier.PropertyChanged -= value; } } }
コンソールにアプリケーションがないかどうかを確認できるコンソールアプリケーションを作成します。
data = new ProxyData(new Data()); (data as INotifyPropertyChanged).PropertyChanged += (s, e) => { Console.WriteLine(string.Format("Property {0} changed!", e.PropertyName)); }; data.Value = 10; data.Source = "List";
長所 :最も純粋なデータクラス、すべてがシンプルで明確であり、さまざまなプロキシロガー、アクセスなどを作成し、アプリケーションの即時操作に関連しないさまざまな機能を組み合わせ、安全に削除および追加できます。
短所 :それは役に立たない、ビューモデルをモデルに変えて、テンプレート-> ViewModel->プロキシ->ビューテンプレートのリンクをもう1つ受け取りました。ViewModelの目的の1つは通知です。さらに、vmの責任は減少したように見えますが、堅実です。
ヒステリー! AOPの時代が来ました。AOPについてはかなり多くのことが書かれています。私は理論については語りません。
私はIUnityContainerと連携しています。 箱入りと見なすことができ、 Prismとうまくやり取りします。
そして、ここに通知者、ホラーの普遍的な行動があります
class NotifyPropertyChangedBehavior : IInterceptionBehavior, INotifyPropertyChanged { static readonly MethodBase _add; static readonly MethodBase _remove; static NotifyPropertyChangedBehavior() { var methods = typeof(INotifyPropertyChanged).GetMethods(); _add = methods[0]; _remove = methods[1]; } public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { IMethodReturn result = null; if (IsPropertyChanged(input)) if (SubscribeUnsubscribe(input)) result = input.CreateMethodReturn(null); else { PropertyInfo property; if (IsSetMethodCalled(out property, input)) result = SetValue(property, input, getNext); } return result ?? getNext()(input, getNext); } public bool WillExecute { get { return true; } } public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// /// </summary> /// <returns></returns> bool IsSetMethodCalled(out PropertyInfo property, IMethodInvocation input) { string propertyName = input.MethodBase.Name.TrimStart("set_".ToArray()); property = input.Target.GetType().GetProperty(propertyName); return property != null; } /// <summary> /// /// </summary> /// <returns></returns> IMethodReturn SetValue(PropertyInfo property, IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { var oldValue = property.GetValue(input.Target, new object[0]); var newValue = input.Arguments[0]; IMethodReturn result = null; // if (!Equals(oldValue, newValue)) { result = getNext()(input, getNext); if (PropertyChanged != null) PropertyChanged(input.Target, new PropertyChangedEventArgs(property.Name)); } else result = input.CreateMethodReturn(null); return result; } /// <summary> /// INotifyPropertyChanged /// </summary> bool SubscribeUnsubscribe(IMethodInvocation input) { if (input.MethodBase == _add) { PropertyChanged += (PropertyChangedEventHandler)input.Arguments[0]; return true; } else if (input.MethodBase == _remove) { PropertyChanged -= (PropertyChangedEventHandler)input.Arguments[0]; return true; } return false; } /// <summary> /// INotifyPropertyChanged /// </summary> bool IsPropertyChanged(IMethodInvocation input) { return input.Target is INotifyPropertyChanged; } }
まあ、すべてが接続するコード
IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.RegisterType<Data>(new Interceptor<VirtualMethodInterceptor>(), new InterceptionBehavior<NotifyPropertyChangedBehavior>() , new AdditionalInterface<INotifyPropertyChanged>()); var data = container.Resolve<Data>(); (data as INotifyPropertyChanged).PropertyChanged += (s, e) => { Console.WriteLine(string.Format("Property {0} changed!", e.PropertyName)); }; data.Value = 10; data.Source = "List"; data.Value = 10; Console.ReadKey();
結果----->

特に怠け者のためのパン
public static class Extensions { public static IUnityContainer RegisterViewModel<T>(this IUnityContainer container) where T : class { container.AddNewExtension<Interception>(); return container.RegisterType<T>(new Interceptor<VirtualMethodInterceptor>(), new InterceptionBehavior<NotifyPropertyChangedBehavior>() , new AdditionalInterface<INotifyPropertyChanged>()); } }
登録の最小化
container.RegisterViewModel<Data>();
幻想を残すことはできません。基本は同じ学位であり、彼については知らないだけです。TssssSSsssssss。
長所 :仮想プロパティ、完全なコード最小化、Real Estate Changedの警告なしのクリーンクラス、あらゆる種類の属性などを備えたすべてのものからVMを作成できるようになりました。
短所 :簡単なことではありません。コンテナとその機能を理解する必要があります。純粋な形での非マニフェスト性、魔法として認識され、多くの反射。
従業員を対象に小さな実験を行った後、結果は嘆かわしく、プログラマーは私がそれがどのように機能するかを理解するように求めたときに苦痛を感じました。
しかし、それは機能します。
一般的に、彼らはそれを導入せず、ほとんど正しい義の火に与えましたが、これはクールですが、それはすべてチームのレベルとトラブルに対する彼らの欲求に依存します。
PS私は、この錫を恐れるすべての人にOnPropertyChanged((Property)の名前) をお勧めします-HANDSで書かれる必要があることを除いて、すべてのインジケーターに最適です。 ブー!