質問するログイン新規登録

質問編集履歴

1

提示いただいた記述の反映

2018/06/30 14:56

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -85,4 +85,116 @@
85
85
 
86
86
  ### 試したこと
87
87
  UserControlのViewModelを削除し、Viewだけにすれば、上記「終了ボタン押下」が実行されるが
88
- UserControlにViewModelがあると、Xxxメソッドが呼び出されない
88
+ UserControlにViewModelがあると、Xxxメソッドが呼び出されない
89
+
90
+ ## 【追記】
91
+ ### コマンド部分を別クラスに分けて MainWindowViewModel / PrismUserControl1ViewModel の両方に注入する方法。
92
+
93
+ UserControlプロジェクトにコマンドのみのクラス追加
94
+ ```C#
95
+ using Reactive.Bindings;
96
+
97
+ namespace PrismUserControl1
98
+ {
99
+ public class FunctionCommand
100
+ {
101
+ public ReactiveCommand CommandExecute { get; set; } = new ReactiveCommand();
102
+ }
103
+ }
104
+ ```
105
+ UserControlのViewModelにあるコンストラクタで上記コマンドのクラスを受け取るようにする
106
+ ```C#
107
+ using Prism.Mvvm;
108
+
109
+ namespace PrismUserControl1.ViewModels
110
+ {
111
+ public class PrismUserControl1ViewModel : BindableBase
112
+ {
113
+ // PrismUserControl1 でバインドして使えるようにアクセサを設ける。
114
+ public FunctionCommand FunctionCommand { get; set; }
115
+
116
+ public PrismUserControl1ViewModel()
117
+ {
118
+ }
119
+
120
+ public PrismUserControl1ViewModel(FunctionCommand functionCommand)
121
+ {
122
+ // アクセサに設定する。
123
+ FunctionCommand = functionCommand;
124
+ }
125
+ }
126
+ }
127
+ ```
128
+
129
+ FormのViewModelもUserControlと同様にコンストラクタでFunctionComandを受け取るようにする
130
+ ```C#
131
+ using Prism.Mvvm;
132
+ using PrismUserControl1;
133
+ using System;
134
+
135
+ namespace UserControlSample.ViewModels
136
+ {
137
+ public class MainWindowViewModel : BindableBase
138
+ {
139
+ // PrismUserControl1 でバインドして使えるようにアクセサを設ける。
140
+ public FunctionCommand FunctionCommand { get; set; }
141
+
142
+ public MainWindowViewModel()
143
+ {
144
+ }
145
+
146
+ public MainWindowViewModel(FunctionCommand functionCommand)
147
+ {
148
+ // アクセサに設定する。
149
+ FunctionCommand = functionCommand;
150
+
151
+ // CommandExecuteに処理のわりあて
152
+ FunctionCommand.CommandExecute.Subscribe(Xxx);
153
+ }
154
+
155
+ private void Xxx()
156
+ {
157
+ Console.WriteLine("ボタン押下");
158
+ }
159
+ }
160
+ }
161
+ ```
162
+ ConfigureContainerでFunctionCommandを注入できるようにする
163
+ ```c#
164
+ using Microsoft.Practices.Unity;
165
+ using Prism.Modularity;
166
+ using Prism.Unity;
167
+ using PrismUserControl1;
168
+ using System.Windows;
169
+ using UserControlSample.Views;
170
+
171
+ namespace UserControlSample
172
+ {
173
+ internal class Bootstrapper : UnityBootstrapper
174
+ {
175
+ protected override DependencyObject CreateShell()
176
+ {
177
+ return Container.Resolve<MainWindow>();
178
+ }
179
+
180
+ protected override void InitializeShell()
181
+ {
182
+ Application.Current.MainWindow.Show();
183
+ }
184
+
185
+ protected override void ConfigureModuleCatalog()
186
+ {
187
+ var moduleCatalog = (ModuleCatalog)ModuleCatalog;
188
+ //moduleCatalog.AddModule(typeof(YOUR_MODULE));
189
+ }
190
+
191
+ protected override void ConfigureContainer()
192
+ {
193
+ base.ConfigureContainer();
194
+
195
+ // FunctionCommandを設定できるようにする
196
+ Container.RegisterType<FunctionCommand>(new ContainerControlledLifetimeManager());
197
+ }
198
+ }
199
+ }
200
+ ```