質問編集履歴

1

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

2018/06/30 14:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -173,3 +173,227 @@
173
173
  UserControlのViewModelを削除し、Viewだけにすれば、上記「終了ボタン押下」が実行されるが
174
174
 
175
175
  UserControlにViewModelがあると、Xxxメソッドが呼び出されない
176
+
177
+
178
+
179
+ ## 【追記】
180
+
181
+ ### コマンド部分を別クラスに分けて MainWindowViewModel / PrismUserControl1ViewModel の両方に注入する方法。
182
+
183
+
184
+
185
+ UserControlプロジェクトにコマンドのみのクラス追加
186
+
187
+ ```C#
188
+
189
+ using Reactive.Bindings;
190
+
191
+
192
+
193
+ namespace PrismUserControl1
194
+
195
+ {
196
+
197
+ public class FunctionCommand
198
+
199
+ {
200
+
201
+ public ReactiveCommand CommandExecute { get; set; } = new ReactiveCommand();
202
+
203
+ }
204
+
205
+ }
206
+
207
+ ```
208
+
209
+ UserControlのViewModelにあるコンストラクタで上記コマンドのクラスを受け取るようにする
210
+
211
+ ```C#
212
+
213
+ using Prism.Mvvm;
214
+
215
+
216
+
217
+ namespace PrismUserControl1.ViewModels
218
+
219
+ {
220
+
221
+ public class PrismUserControl1ViewModel : BindableBase
222
+
223
+ {
224
+
225
+ // PrismUserControl1 でバインドして使えるようにアクセサを設ける。
226
+
227
+ public FunctionCommand FunctionCommand { get; set; }
228
+
229
+
230
+
231
+ public PrismUserControl1ViewModel()
232
+
233
+ {
234
+
235
+ }
236
+
237
+
238
+
239
+ public PrismUserControl1ViewModel(FunctionCommand functionCommand)
240
+
241
+ {
242
+
243
+ // アクセサに設定する。
244
+
245
+ FunctionCommand = functionCommand;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ }
252
+
253
+ ```
254
+
255
+
256
+
257
+ FormのViewModelもUserControlと同様にコンストラクタでFunctionComandを受け取るようにする
258
+
259
+ ```C#
260
+
261
+ using Prism.Mvvm;
262
+
263
+ using PrismUserControl1;
264
+
265
+ using System;
266
+
267
+
268
+
269
+ namespace UserControlSample.ViewModels
270
+
271
+ {
272
+
273
+ public class MainWindowViewModel : BindableBase
274
+
275
+ {
276
+
277
+ // PrismUserControl1 でバインドして使えるようにアクセサを設ける。
278
+
279
+ public FunctionCommand FunctionCommand { get; set; }
280
+
281
+
282
+
283
+ public MainWindowViewModel()
284
+
285
+ {
286
+
287
+ }
288
+
289
+
290
+
291
+ public MainWindowViewModel(FunctionCommand functionCommand)
292
+
293
+ {
294
+
295
+ // アクセサに設定する。
296
+
297
+ FunctionCommand = functionCommand;
298
+
299
+
300
+
301
+ // CommandExecuteに処理のわりあて
302
+
303
+ FunctionCommand.CommandExecute.Subscribe(Xxx);
304
+
305
+ }
306
+
307
+
308
+
309
+ private void Xxx()
310
+
311
+ {
312
+
313
+ Console.WriteLine("ボタン押下");
314
+
315
+ }
316
+
317
+ }
318
+
319
+ }
320
+
321
+ ```
322
+
323
+ ConfigureContainerでFunctionCommandを注入できるようにする
324
+
325
+ ```c#
326
+
327
+ using Microsoft.Practices.Unity;
328
+
329
+ using Prism.Modularity;
330
+
331
+ using Prism.Unity;
332
+
333
+ using PrismUserControl1;
334
+
335
+ using System.Windows;
336
+
337
+ using UserControlSample.Views;
338
+
339
+
340
+
341
+ namespace UserControlSample
342
+
343
+ {
344
+
345
+ internal class Bootstrapper : UnityBootstrapper
346
+
347
+ {
348
+
349
+ protected override DependencyObject CreateShell()
350
+
351
+ {
352
+
353
+ return Container.Resolve<MainWindow>();
354
+
355
+ }
356
+
357
+
358
+
359
+ protected override void InitializeShell()
360
+
361
+ {
362
+
363
+ Application.Current.MainWindow.Show();
364
+
365
+ }
366
+
367
+
368
+
369
+ protected override void ConfigureModuleCatalog()
370
+
371
+ {
372
+
373
+ var moduleCatalog = (ModuleCatalog)ModuleCatalog;
374
+
375
+ //moduleCatalog.AddModule(typeof(YOUR_MODULE));
376
+
377
+ }
378
+
379
+
380
+
381
+ protected override void ConfigureContainer()
382
+
383
+ {
384
+
385
+ base.ConfigureContainer();
386
+
387
+
388
+
389
+ // FunctionCommandを設定できるようにする
390
+
391
+ Container.RegisterType<FunctionCommand>(new ContainerControlledLifetimeManager());
392
+
393
+ }
394
+
395
+ }
396
+
397
+ }
398
+
399
+ ```