通常であればコード(1)のようにModel
がINotifyPropertyChanged
を実装していれば、INotifyPropertyChanged
の拡張メソッドであるToReactivePropertyAsSynchronized()
を使用できます。
しかし、今書いているコード(2)は事情によりINotifyPropertyChanged
が使用できません。
そのため、代わりにContentChanged
というイベントを作り、それを元にModel
→ViewModel
へ片方向同期をするReactiveProperty
を作り、さらにそのReactiveProperty
の変更を監視してModel
に書き戻す、という不格好なコードになってしまっています。
実際の動作は変わらないように見えますが、より良い方法があれば教えてください。
C#
1using Reactive.Bindings; 2using Reactive.Bindings.Extensions; 3using System; 4using System.ComponentModel; 5 6namespace ConsoleApp1 7{ 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 var model = new Model(); 13 var viewModel = new ViewModel(model); 14 15 // ViewにBindingのつもり 16 viewModel.Content.Subscribe(x => Console.WriteLine($"ViewModel's Content was changed to: {x}")); 17 18 // Modelの変更 19 model.Content = "あいうえお"; 20 21 // ViewModelの変更 22 viewModel.Content.Value = "かきくけこ"; 23 24 Console.ReadKey(); 25 26 /* 実行結果: 27 Model's Content was changed to: 28 ViewModel's Content was changed to: 29 Model's Content was changed to: あいうえお 30 Model's Content was changed to: あいうえお 31 ViewModel's Content was changed to: あいうえお 32 Model's Content was changed to: かきくけこ 33 ViewModel's Content was changed to: かきくけこ 34 */ 35 } 36 } 37 38 class Model : INotifyPropertyChanged 39 { 40 public event PropertyChangedEventHandler PropertyChanged; 41 42 private string content; 43 public string Content 44 { 45 get 46 { 47 return content; 48 } 49 set 50 { 51 content = value; 52 Console.WriteLine($"Model's Content was changed to: {value}"); 53 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content))); 54 } 55 } 56 } 57 58 class ViewModel 59 { 60 public ViewModel(Model model) 61 { 62 // Model<-->ViewModelの双方向同期をするReactiveProperty 63 Content = model.ToReactivePropertyAsSynchronized(x => x.Content); 64 } 65 66 public ReactiveProperty<string> Content { get; } 67 } 68} 69
C#
1using Reactive.Bindings; 2using System; 3using System.Reactive.Linq; 4 5namespace ConsoleApp2 6{ 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 var model = new Model(); 12 var viewModel = new ViewModel(model); 13 14 // ViewにBindingのつもり 15 viewModel.Content.Subscribe(x => Console.WriteLine($"ViewModel's Content was changed to: {x}")); 16 17 // Modelの変更 18 model.Content = "あいうえお"; 19 20 // ViewModelの変更 21 viewModel.Content.Value = "かきくけこ"; 22 23 Console.ReadKey(); 24 25 /* 実行結果: 26 Model's Content was changed to: 27 ViewModel's Content was changed to: 28 Model's Content was changed to: あいうえお 29 Model's Content was changed to: あいうえお 30 ViewModel's Content was changed to: あいうえお 31 Model's Content was changed to: かきくけこ 32 ViewModel's Content was changed to: かきくけこ 33 */ 34 } 35 } 36 37 class Model 38 { 39 public event EventHandler<string> ContentChanged; 40 41 private string content; 42 public string Content 43 { 44 get 45 { 46 return content; 47 } 48 set 49 { 50 content = value; 51 Console.WriteLine($"Model's Content was changed to: {value}"); 52 ContentChanged?.Invoke(this, value); 53 } 54 } 55 } 56 57 class ViewModel 58 { 59 public ViewModel(Model model) 60 { 61 // Model->ViewModelの片方向同期をするReactiveProperty 62 Content = Observable.FromEvent<EventHandler<string>, string>(h => (s, e) => h(e), h => model.ContentChanged += h, h => model.ContentChanged -= h) 63 .ToReactiveProperty(); 64 65 // ReactivePropertyの変更をViewModel->Modelに反映する 66 Content.Subscribe(x => model.Content = x); 67 } 68 69 public ReactiveProperty<string> Content { get; } 70 } 71} 72
回答1件
あなたの回答
tips
プレビュー