回答編集履歴

1

見直しキャンペーン中

2023/07/27 13:53

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,305 +1,153 @@
1
1
  > 個人的にはAutoWireしなければ親のDataContextを引き継ぎますから、どうにでもコントロールできていいと思うのですが(Prism的にはいまいちなんですかね)
2
-
3
2
  >
4
-
5
3
  > あとd:DataContextにデザイン用VMなりVMでデザイン時を判断しデータを入れるようにしておくと気が利くなって思います。
6
4
 
7
-
8
-
9
5
  回答の私のコメントを回収しておきます。
10
-
11
6
  VMの条件によっては`AutoWire`してもいいと思います。
12
-
13
7
  `SetDefaultViewTypeToViewModelTypeResolver`が面倒だったら、本体の`ViewAViewModel`をカラ継承しておいておくとか?^^;
14
-
15
-
16
8
 
17
9
  本体はプラグインのことを知らないが、プラグインは本体のことを知っていていいんですよね?
18
10
 
19
11
 
20
-
21
-
22
-
23
12
  本体側
24
-
25
- ```xaml
13
+ ```xml
26
-
27
14
  <Window
28
-
29
15
  x:Class="BlankCoreApp1.Views.MainWindow"
30
-
31
16
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
32
-
33
17
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
34
-
35
18
  xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
36
-
37
19
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
38
-
39
20
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
40
-
41
21
  xmlns:prism="http://prismlibrary.com/"
42
-
43
22
  Width="525"
44
-
45
23
  Height="350"
46
-
47
24
  d:DataContext="{d:DesignInstance {x:Type ViewModels:MainWindowViewModel}}"
48
-
49
25
  prism:ViewModelLocator.AutoWireViewModel="True"
50
-
51
26
  mc:Ignorable="d">
52
-
53
27
  <Grid>
54
-
55
28
  <GroupBox Header="ViewA">
56
-
57
29
  <ContentControl prism:RegionManager.RegionName="ContentRegion" DataContext="{Binding ViewAViewModel}" />
58
-
59
30
  </GroupBox>
60
-
61
31
  </Grid>
62
-
63
32
  </Window>
64
-
65
33
  ```
66
34
 
67
-
68
-
69
- ```xaml
35
+ ```xml
70
-
71
36
  <UserControl
72
-
73
37
  x:Class="BlankCoreApp1.Views.DefaultViewA"
74
-
75
38
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
76
-
77
39
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
78
-
79
40
  xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
80
-
81
41
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
82
-
83
42
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
84
-
85
43
  d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
86
-
87
44
  mc:Ignorable="d">
88
-
89
45
  <DockPanel>
90
-
91
46
  <TextBlock DockPanel.Dock="Top" Text="BlankCoreApp1.Views.DefaultViewA" />
92
-
93
47
  <TextBlock Text="{Binding Message}" />
94
-
95
48
  </DockPanel>
96
-
97
49
  </UserControl>
98
-
99
50
  ```
100
51
 
101
-
102
-
103
- ```C#
52
+ ```cs
104
-
105
53
  using BlankCoreApp1.Views;
106
-
107
54
  using Prism.Mvvm;
108
-
109
55
  using Prism.Regions;
110
56
 
111
-
112
-
113
57
  namespace BlankCoreApp1.ViewModels
114
-
115
58
  {
116
-
117
59
  public class MainWindowViewModel : BindableBase
118
-
119
60
  {
120
-
121
61
  // 本題でないので直接newするが好きにDIしてください
122
-
123
62
  public ViewAViewModel ViewAViewModel { get; } = new ViewAViewModel();
124
-
125
-
126
63
 
127
64
  public MainWindowViewModel() { }
128
65
 
66
+ public MainWindowViewModel(IRegionManager regionManager)
67
+ {
68
+ // CustomViewがなければデフォルトを表示
69
+ regionManager.RegisterViewWithRegion("ContentRegion", typeof(DefaultViewA));
70
+ }
71
+ }
129
72
 
73
+ public class ViewAViewModel : BindableBase
74
+ {
75
+ private string _message;
76
+ public string Message { get => _message; set => SetProperty(ref _message, value); }
130
77
 
131
- public MainWindowViewModel(IRegionManager regionManager)
78
+ public ViewAViewModel() => Message = $"{GetType().FullName}";
79
+ }
80
+ }
81
+ ```
132
82
 
83
+ ```cs
84
+ using BlankCoreApp1.Views;
85
+ using Prism.Ioc;
86
+ using Prism.Modularity;
87
+ using System.Windows;
88
+
89
+ namespace BlankCoreApp1
90
+ {
91
+ public partial class App
92
+ {
93
+ protected override Window CreateShell() => Container.Resolve<MainWindow>();
94
+
95
+ protected override IModuleCatalog CreateModuleCatalog()
133
96
  {
134
-
135
- // CustomViewがなければデフォルトを表示
97
+ // 指定フォルダからモジュール読み込み
136
-
137
- regionManager.RegisterViewWithRegion("ContentRegion", typeof(DefaultViewA));
98
+ return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
138
-
139
99
  }
140
100
 
101
+ protected override void RegisterTypes(IContainerRegistry containerRegistry) { }
141
102
  }
142
-
143
-
144
-
145
- public class ViewAViewModel : BindableBase
146
-
147
- {
148
-
149
- private string _message;
150
-
151
- public string Message { get => _message; set => SetProperty(ref _message, value); }
152
-
153
-
154
-
155
- public ViewAViewModel() => Message = $"{GetType().FullName}";
156
-
157
- }
158
-
159
103
  }
160
-
161
104
  ```
162
-
163
-
164
-
165
- ```C#
166
-
167
- using BlankCoreApp1.Views;
168
-
169
- using Prism.Ioc;
170
-
171
- using Prism.Modularity;
172
-
173
- using System.Windows;
174
-
175
-
176
-
177
- namespace BlankCoreApp1
178
-
179
- {
180
-
181
- public partial class App
182
-
183
- {
184
-
185
- protected override Window CreateShell() => Container.Resolve<MainWindow>();
186
-
187
-
188
-
189
- protected override IModuleCatalog CreateModuleCatalog()
190
-
191
- {
192
-
193
- // 指定フォルダからモジュール読み込み
194
-
195
- return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
196
-
197
- }
198
-
199
-
200
-
201
- protected override void RegisterTypes(IContainerRegistry containerRegistry) { }
202
-
203
- }
204
-
205
- }
206
-
207
- ```
208
-
209
-
210
105
 
211
106
  ---
212
107
 
213
-
214
-
215
108
  プラグイン側
216
-
217
- ```xaml
109
+ ```xml
218
-
219
110
  <UserControl
220
-
221
111
  x:Class="CustomViewAModule.Views.ViewA"
222
-
223
112
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
224
-
225
113
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
226
-
227
114
  xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels;assembly=BlankCoreApp1"
228
-
229
115
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
230
-
231
116
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
232
-
233
117
  d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
234
-
235
118
  mc:Ignorable="d">
236
-
237
119
  <DockPanel>
238
-
239
120
  <TextBlock DockPanel.Dock="Top" Text="CustomViewAModule.Views.ViewA" />
240
-
241
121
  <TextBlock Text="{Binding Message}" />
242
-
243
122
  </DockPanel>
244
-
245
123
  </UserControl>
246
-
247
124
  ```
248
125
 
249
-
250
-
251
- ```C#
126
+ ```cs
252
-
253
127
  using CustomViewAModule.Views;
254
-
255
128
  using Prism.Ioc;
256
-
257
129
  using Prism.Modularity;
258
-
259
130
  using Prism.Regions;
260
131
 
261
-
262
-
263
132
  namespace CustomViewAModule
264
-
265
133
  {
266
-
267
134
  public class CustomViewAModuleModule : IModule
268
-
269
135
  {
270
-
271
136
  public void OnInitialized(IContainerProvider containerProvider)
272
-
273
137
  {
274
-
275
138
  var regionManager = containerProvider.Resolve<IRegionManager>();
276
139
 
277
-
278
-
279
140
  // CustomViewで上書きナビゲート
280
-
281
141
  regionManager.RequestNavigate("ContentRegion", "ViewA");
282
142
 
283
-
284
-
285
143
  // こちらが後に呼ばれているのにうまく出なかった
286
-
287
144
  //regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
288
-
289
145
  }
290
146
 
291
-
292
-
293
147
  public void RegisterTypes(IContainerRegistry containerRegistry)
294
-
295
148
  => containerRegistry.RegisterForNavigation<ViewA>();
296
-
297
149
  }
298
-
299
150
  }
300
-
301
151
  ```
302
152
 
303
-
304
-
305
153
  ![アプリ画像](c0fd8a2818a07b8e5271510011e458de.png)