質問編集履歴
4
質問内容を整理しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,50 +1,28 @@
|
|
1
1
|
###前提・実現したいこと
|
2
|
-
C#で、MVVMを使用したプログラムを作成したいと思います。
|
3
|
-
複数の解釈とコード例が検索されますが、なか
|
2
|
+
MVVMで、Web検索しますと、複数の解釈とコード例が検索されますが、基本的な例が見つかりません。
|
4
|
-
|
3
|
+
そこで、MS社の資料を参考にC# Prism Unityで、MVVM構造の単純なプログラムを作成しました。
|
5
4
|
|
5
|
+
<作成内容>
|
6
|
-
「テキストボックスに
|
6
|
+
「各テキストボックスに、ある数Aとある数Bを入力し、ボタンを押すとラベルに結果を表示する。」
|
7
7
|
|
8
8
|

|
9
9
|
|
10
|
+
<課題>
|
10
|
-
|
11
|
+
素直に、そのまま作りますと、只の足し算が信じられない複雑なプログラムになりました。
|
12
|
+
簡単なプログラムにする良い方法を検討しています。
|
11
13
|
|
12
|
-
ここに質問したいことを詳細に書いてください
|
14
|
+
###ここに質問したいことを詳細に書いてください
|
13
15
|
|
14
|
-
MSDNの説明は、応用的、基本が見えないと思います。そこで
|
15
|
-
|
16
|
+
C# Prism Unityで、MVVM構造プログラムを作成する場合、定石的な方法で、簡単なコードをご教授下さい。
|
16
17
|
|
17
|
-
###発生している問題・エラーメッセージ
|
18
|
-
1.View+ViewModelだけで、MVVM
|
19
|
-
2.View+ViewModel+Model
|
20
|
-
3.View+ViewModel+Control
|
21
|
-
4.View+ViewModel+プレゼンテーション
|
22
|
-
なぜか、使いこなすには至らない。
|
23
|
-
ここで、これが、良いパターンとは、説明できない。
|
24
|
-
複雑過ぎではないかなぁ工数を費やす気になれないのは私だけでしょうか?
|
25
|
-
|
26
|
-
エラーメッセージ
|
27
|
-
1.モジュール分け、ファイル分割で、プログラムの動作が見えなくなる。
|
28
|
-
2.工数が増大する。
|
29
|
-
3.保守が難しい。
|
30
|
-
|
31
18
|
###該当のソースコード
|
32
|
-
この例が一番良いと思います。
|
33
|
-
しかし、最新のメソッドを使用している様子、基本的では無いのでは
|
34
|
-
と思い質問しました。
|
35
|
-
http://qiita.com/hiki_neet_p/items/e381c687b0644c0e4978
|
36
19
|
|
37
|
-
###試したこと
|
38
|
-
ToReactivePropertyAsSynchronized()の調査
|
39
|
-
只の足し算でも、ここまでになるのは理解に苦しむ。
|
40
|
-
|
41
|
-
###補足情報(言語/FW/ツール等のバージョンなど)
|
42
20
|
マイクロソフト社の解説書
|
43
21
|
CompositeWPF-June2008-JA-JP.chm
|
44
22
|
MVVM関連
|
45
23
|
https://www.microsoft.com/ja-JP/download/details.aspx?id=39042
|
46
24
|
|
47
|
-
(基本プログラム
|
25
|
+
(複雑になった基本プログラム)
|
48
26
|

|
49
27
|
|
50
28
|
1.準備
|
3
コードを追加する。
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,4 +42,235 @@
|
|
42
42
|
マイクロソフト社の解説書
|
43
43
|
CompositeWPF-June2008-JA-JP.chm
|
44
44
|
MVVM関連
|
45
|
-
https://www.microsoft.com/ja-JP/download/details.aspx?id=39042
|
45
|
+
https://www.microsoft.com/ja-JP/download/details.aspx?id=39042
|
46
|
+
|
47
|
+
(基本プログラム作成)
|
48
|
+

|
49
|
+
|
50
|
+
1.準備
|
51
|
+
VisualStudioCommunity2015
|
52
|
+
wpfアプリケーションのプロジェクト
|
53
|
+
NuGetパッケージ管理より
|
54
|
+
Prism、Prism.Unitをインストール
|
55
|
+
|
56
|
+
2.Composite Application Libraryを学ぶ
|
57
|
+
2-1 App.XAML
|
58
|
+
```XAML言語
|
59
|
+
<Application x:Class="WpfMVVMPrism.App"
|
60
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
61
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
62
|
+
xmlns:local="clr-namespace:WpfMVVMPrism"
|
63
|
+
Startup ="Application_Startup">
|
64
|
+
<Application.Resources>
|
65
|
+
</Application.Resources>
|
66
|
+
</Application>
|
67
|
+
```
|
68
|
+
2-2 App.XAML.cs
|
69
|
+
```C#言語
|
70
|
+
using System.Windows;
|
71
|
+
namespace WpfMVVMPrism
|
72
|
+
{
|
73
|
+
/// <summary>
|
74
|
+
/// App.xaml の相互作用ロジック
|
75
|
+
/// </summary>
|
76
|
+
public partial class App : Application
|
77
|
+
{
|
78
|
+
private void Application_Startup(object sender, StartupEventArgs e)
|
79
|
+
{
|
80
|
+
var boot = new Bootstrap();
|
81
|
+
boot.Run();
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
2-3 Bootstrap.cs Shellをコンテナに登録(最小コード)
|
87
|
+
```C#言語
|
88
|
+
using System.Windows;
|
89
|
+
using Microsoft.Practices.Unity;
|
90
|
+
using Prism.Mvvm;
|
91
|
+
namespace WpfMVVMPrism
|
92
|
+
{
|
93
|
+
/// <summary>
|
94
|
+
/// Unity コンテナーを使用する。
|
95
|
+
/// </summary>
|
96
|
+
public class Bootstrap : Prism.Unity.UnityBootstrapper
|
97
|
+
{
|
98
|
+
protected override DependencyObject CreateShell()
|
99
|
+
{
|
100
|
+
return this.Container.Resolve<Shell>();
|
101
|
+
}
|
102
|
+
|
103
|
+
protected override void InitializeShell()
|
104
|
+
{
|
105
|
+
ViewModelLocator.SetAutoWireViewModel(this.Shell, true);
|
106
|
+
((Shell)this.Shell).Show();
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
```
|
111
|
+
2-4 Shell.XAML
|
112
|
+
```XAML言語
|
113
|
+
<Window
|
114
|
+
x:Class="WpfMVVMPrism.Shell"
|
115
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
116
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
117
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
118
|
+
xmlns:local="clr-namespace:WpfMVVMPrism"
|
119
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
120
|
+
Title="MVVMの基本その1(計算 A+B=C)"
|
121
|
+
Width="350"
|
122
|
+
Height="200"
|
123
|
+
mc:Ignorable="d">
|
124
|
+
|
125
|
+
<!-- <Window.DataContext>
|
126
|
+
<local:ViewModel />
|
127
|
+
</Window.DataContext> -->
|
128
|
+
|
129
|
+
<Grid>
|
130
|
+
<Grid.ColumnDefinitions>
|
131
|
+
<ColumnDefinition />
|
132
|
+
</Grid.ColumnDefinitions>
|
133
|
+
|
134
|
+
<Grid.RowDefinitions>
|
135
|
+
<RowDefinition />
|
136
|
+
<RowDefinition />
|
137
|
+
<RowDefinition />
|
138
|
+
<RowDefinition />
|
139
|
+
</Grid.RowDefinitions>
|
140
|
+
<TextBox
|
141
|
+
x:Name="textBoxA"
|
142
|
+
Grid.Row="0"
|
143
|
+
Text="{Binding DataA}" />
|
144
|
+
<TextBox
|
145
|
+
x:Name="textBoxB"
|
146
|
+
Grid.Row="1"
|
147
|
+
Text="{Binding DataB}" />
|
148
|
+
<Button
|
149
|
+
x:Name="button"
|
150
|
+
Grid.Row="2"
|
151
|
+
Content="「A+B=C」の計算をする"
|
152
|
+
Command="{Binding CalculationCommand}"/>
|
153
|
+
<Label
|
154
|
+
x:Name="labelC"
|
155
|
+
Grid.Row="3"
|
156
|
+
Content="{Binding ResultC}" />
|
157
|
+
</Grid>
|
158
|
+
</Window>
|
159
|
+
```
|
160
|
+
2-5 Shell.XAML.CS
|
161
|
+
```C#言語
|
162
|
+
using System.Windows;
|
163
|
+
namespace WpfMVVMPrism
|
164
|
+
{
|
165
|
+
/// <summary>
|
166
|
+
/// Shell.xaml の相互作用ロジック
|
167
|
+
/// </summary>
|
168
|
+
public partial class Shell : Window
|
169
|
+
{
|
170
|
+
public Shell()
|
171
|
+
{
|
172
|
+
InitializeComponent();
|
173
|
+
this.DataContext = new ViewModel();
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
```
|
178
|
+
2-6 VeiwModel.cs
|
179
|
+
```C#言語
|
180
|
+
using System;
|
181
|
+
using System.ComponentModel;
|
182
|
+
using System.Windows.Input;
|
183
|
+
namespace WpfMVVMPrism
|
184
|
+
{
|
185
|
+
public class ViewModel : INotifyPropertyChanged
|
186
|
+
{
|
187
|
+
public int DataA { get; set; }
|
188
|
+
public int DataB { get; set; }
|
189
|
+
|
190
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
191
|
+
private int _resultC;
|
192
|
+
public int ResultC
|
193
|
+
{
|
194
|
+
get
|
195
|
+
{
|
196
|
+
return _resultC;
|
197
|
+
}
|
198
|
+
set
|
199
|
+
{
|
200
|
+
_resultC = value;
|
201
|
+
OnPropertyChanged("ResultC");
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
protected void OnPropertyChanged(string name)
|
206
|
+
{
|
207
|
+
PropertyChangedEventHandler handler = PropertyChanged;
|
208
|
+
if (handler != null)
|
209
|
+
{
|
210
|
+
handler(this, new PropertyChangedEventArgs(name));
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
private ICommand _calculationCommand;
|
215
|
+
public ICommand CalculationCommand
|
216
|
+
{
|
217
|
+
get
|
218
|
+
{
|
219
|
+
if (_calculationCommand == null)
|
220
|
+
{
|
221
|
+
_calculationCommand = new RelayCommand(ExecuteCalculationCommand);
|
222
|
+
}
|
223
|
+
return _calculationCommand;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
private void ExecuteCalculationCommand(object x)
|
228
|
+
{
|
229
|
+
Model m = new Model();
|
230
|
+
m.a_val = DataA;
|
231
|
+
m.b_val = DataB;
|
232
|
+
ResultC = m.SumAB();
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
internal class RelayCommand : ICommand
|
237
|
+
{
|
238
|
+
private Action<object> executeCalculationCommand;
|
239
|
+
|
240
|
+
public RelayCommand(Action<object> executeCalculationCommand)
|
241
|
+
{
|
242
|
+
this.executeCalculationCommand = executeCalculationCommand;
|
243
|
+
}
|
244
|
+
|
245
|
+
public event EventHandler CanExecuteChanged;
|
246
|
+
|
247
|
+
public bool CanExecute(object parameter)
|
248
|
+
{
|
249
|
+
return true;
|
250
|
+
}
|
251
|
+
|
252
|
+
public void Execute(object parameter)
|
253
|
+
{
|
254
|
+
this.executeCalculationCommand(parameter);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
```
|
259
|
+
2-7 Model.cs
|
260
|
+
```C#言語
|
261
|
+
namespace WpfMVVMPrism
|
262
|
+
{
|
263
|
+
public class Model
|
264
|
+
{
|
265
|
+
public int a_val;
|
266
|
+
public int b_val;
|
267
|
+
public int c_val;
|
268
|
+
|
269
|
+
public int SumAB()
|
270
|
+
{
|
271
|
+
return c_val = a_val + b_val;
|
272
|
+
}
|
273
|
+
}
|
274
|
+
}
|
275
|
+
```
|
276
|
+
以上です。
|
2
コードを追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
イメージ図を作成して見ました
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
「テキストボックスにそれぞれ、ある数Aとある数Bを入力し、ボタンを押すとラベルに結果を表示する。」
|
7
7
|
|
8
|
+

|
9
|
+
|
8
10
|
これを、Prismを使って、Boost View、ViewModel Model に分離し、大企業向けの開発体制を考慮し作成すると、只の足し算が信じられない複雑なプログラムになります。
|
9
11
|
|
10
12
|
ここに質問したいことを詳細に書いてください
|