Page1.xamlに紐づくPage1ViewModelと、Page2.xamlに紐づくPage2ViewModelが存在していて、
Page1の入力値をPage1ViewModel→Modelに反映して、
Modelの変更通知をPage2ViewModelで受け取ってPage2に反映したいです。
(Page1→Page1ViewModel→Model→Page2ViewModel→Page2という値の伝播のイメージ)
具体的にやりたい事は画面左側のPage1上のTextBoxに入力した値をVMとMを経由して、
右側のPage2のTextBlockに表示させたいです。
以前ここでさせていただいた質問で、
ModelをSingletonにすることでPage1ViewModelとPage2ViewModelから参照可能と教えていただきました。
早速試してみているのですが、うまく動きません。
シングルトンが初めてなことと、
ModelにReactivePropertyを使用することも初めてなことが相まって
どちらのコードにミスがあるのか分からない状態です。
xaml
1MainWindow.xaml 2 <Grid> 3 <Grid.ColumnDefinitions> 4 <ColumnDefinition /> 5 <ColumnDefinition /> 6 </Grid.ColumnDefinitions> 7 <Frame Source="Page1.xaml"></Frame> 8 <Frame Source="Page2.xaml" Grid.Column="1"></Frame> 9 </Grid>
xaml
1Page1.xaml 2 <Grid> 3 <TextBox Width="100" 4 Height="100" 5 Text="{Binding TextInput.Value , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox> 6 </Grid>
C#
1Page1.xaml.cs 2 public partial class Page1 : Page 3 { 4 public Page1() 5 { 6 InitializeComponent(); 7 var Data = new Page1ViewModel(Model.GetInstance()); 8 DataContext = Data; 9 } 10 }
xaml
1Page2.xaml 2 <Grid> 3 <TextBlock Width="100" 4 Height="100" 5 Background="LightBlue" 6 Text="{Binding TextOutput.Value , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock> 7 </Grid>
C#
1Page2.xaml.cs 2 public partial class Page2 : Page 3 { 4 public Page2() 5 { 6 InitializeComponent(); 7 var Data = new Page2ViewModel(Model.GetInstance()); 8 DataContext = Data; 9 } 10 }
C#
1Page1ViewModel.cs 2 class Page1ViewModel 3 { 4 ReactiveProperty<String> TextInput { get; set; } = new ReactiveProperty<String>(); 5 public Model Model { get; } 6 7 public Page1ViewModel(Model model) 8 { 9 this.Model = model; 10 this.TextInput = Model.Input.ToReactivePropertyAsSynchronized(x => x.Value); 11 } 12 }
C#
1Page2ViewModel.cs 2 class Page2ViewModel 3 { 4 ReactiveProperty<String> TextOutput { get; set; } 5 public Model Model { get; } 6 7 public Page2ViewModel(Model model) 8 { 9 this.Model = model; 10 this.TextOutput = model.Input.ToReactivePropertyAsSynchronized(x => x.Value); 11 } 12 }
C#
1Model.cs 2 class Model 3 { 4 private static Model instance = new Model(); 5 6 public ReactiveProperty<string> Input 7 { 8 get; set; 9 } 10 11 private Model () 12 { 13 Input = new ReactiveProperty<string>("a"); 14 } 15 16 public static Model GetInstance() 17 { 18 if (instance == null) 19 instance = new Model(); 20 return instance; 21 } 22 }
以下の2行が怪しそうだな(ReactivePropertyをToReactivePropertyしている?)とは思い
色々変えてみても動かすことができず質問させていただきました。
this.TextOutput = model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
this.TextInput = Model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/12 14:24
2018/11/13 00:29
2018/11/13 14:29