回答編集履歴
2
見直しキャンペーン中
test
CHANGED
@@ -1,457 +1,220 @@
|
|
1
1
|
なんか堂々巡りですね^^;
|
2
|
-
|
3
2
|
どうもprismユーザーと話が合わないのは、私がわかってないだけなんでしょうか?(だったらいいのですが^^;
|
4
3
|
|
5
|
-
|
6
|
-
|
7
4
|
理屈上は`RelativeSource`で`Window`とでもすれば引っ張ってこれますが、そんなのイヤすぎますよね?
|
8
5
|
|
9
|
-
|
10
|
-
|
11
6
|
そもそもガッツリprismしている場合、`UserControl`の表示は↓のようになりませんか?(Prism Full App(.NET Core)テンプレートより)
|
12
|
-
|
13
|
-
```x
|
7
|
+
```xml
|
14
|
-
|
15
8
|
<ContentControl prism:RegionManager.RegionName="{x:Static core:RegionNames.ContentRegion}" />
|
16
|
-
|
17
|
-
```
|
9
|
+
```
|
18
|
-
|
19
10
|
`DependencyProperty`を設定するタイミングもないです(まあやりようはあるんでしょうけど)
|
20
11
|
|
21
|
-
|
22
|
-
|
23
12
|
一度Prism Full App(.NET Core)テンプレートを作っていただいて、`ViewA`・`ViewAViewModel`・`MessageService`の関係を見ていただけたらいいと思います。
|
24
13
|
|
25
|
-
|
26
|
-
|
27
14
|
---
|
28
15
|
|
29
|
-
|
30
|
-
|
31
16
|
例がないと話の前提が合いませんので雑に作りました(`Func<T,R>`が気持ち悪いのは気にしないでください^^;
|
32
|
-
|
33
17
|
[NuGet Gallery | Prism.DryIoc 8.1.97](https://www.nuget.org/packages/Prism.DryIoc/8.1.97)
|
34
|
-
|
35
18
|
[NuGet Gallery | ReactiveProperty 7.12.0](https://www.nuget.org/packages/ReactiveProperty/7.12.0)
|
36
19
|
|
37
|
-
|
38
|
-
|
39
20
|
`TextBox`と`TextBlock`の組が4つあります。どれも動作は同じです(入力値の小文字が大文字になって表示される)
|
40
21
|
|
41
|
-
|
42
|
-
|
43
22
|
4つのうち1つだけおかしい(異質な)ものがあります。
|
44
23
|
|
45
|
-
|
46
|
-
|
47
|
-
MainWindow.xaml
|
24
|
+
```xml:MainWindow.xaml
|
48
|
-
|
49
|
-
```xaml
|
50
|
-
|
51
25
|
<Window
|
52
|
-
|
53
26
|
x:Class="Questions362091.Views.MainWindow"
|
54
|
-
|
55
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
27
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
56
|
-
|
57
28
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
58
|
-
|
59
29
|
xmlns:prism="http://prismlibrary.com/"
|
60
|
-
|
61
30
|
xmlns:views="clr-namespace:Questions362091.Views"
|
62
|
-
|
63
31
|
Width="525"
|
64
|
-
|
65
32
|
SizeToContent="Height">
|
66
|
-
|
67
|
-
<StackPanel>
|
33
|
+
<StackPanel>
|
68
|
-
|
69
|
-
|
70
34
|
|
71
35
|
<GroupBox Header="ベタ置き">
|
72
|
-
|
73
36
|
<StackPanel>
|
74
|
-
|
75
37
|
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
76
|
-
|
77
38
|
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
78
|
-
|
79
39
|
<TextBlock Text="{Binding Output.Value}" />
|
80
|
-
|
81
40
|
</StackPanel>
|
82
|
-
|
83
|
-
</GroupBox>
|
41
|
+
</GroupBox>
|
84
|
-
|
85
|
-
|
86
42
|
|
87
43
|
<GroupBox Header="UserControl1">
|
88
|
-
|
89
44
|
<views:UserControl1 />
|
90
|
-
|
91
|
-
</GroupBox>
|
45
|
+
</GroupBox>
|
92
|
-
|
93
|
-
|
94
46
|
|
95
47
|
<GroupBox Header="PrismUserControl1">
|
96
|
-
|
97
48
|
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
|
98
|
-
|
99
|
-
</GroupBox>
|
49
|
+
</GroupBox>
|
100
|
-
|
101
|
-
|
102
50
|
|
103
51
|
<GroupBox Header="UserControl2">
|
104
|
-
|
105
52
|
<views:UserControl2 Converter="{Binding Converter}" Input="{Binding Input.Value}" />
|
106
|
-
|
107
|
-
</GroupBox>
|
53
|
+
</GroupBox>
|
108
|
-
|
109
|
-
</StackPanel>
|
54
|
+
</StackPanel>
|
110
|
-
|
111
55
|
</Window>
|
112
|
-
|
113
|
-
```
|
56
|
+
```
|
114
|
-
|
115
|
-
|
116
|
-
|
57
|
+
|
117
|
-
UserControl1.xaml
|
58
|
+
```xml:UserControl1.xaml
|
118
|
-
|
119
|
-
```xaml
|
120
|
-
|
121
59
|
<UserControl
|
122
|
-
|
123
60
|
x:Class="Questions362091.Views.UserControl1"
|
124
|
-
|
125
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
61
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
126
|
-
|
127
62
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
128
|
-
|
129
|
-
<StackPanel>
|
63
|
+
<StackPanel>
|
130
|
-
|
131
64
|
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
132
|
-
|
133
65
|
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
134
|
-
|
135
66
|
<TextBlock Text="{Binding Output.Value}" />
|
136
|
-
|
137
|
-
</StackPanel>
|
67
|
+
</StackPanel>
|
138
|
-
|
139
68
|
</UserControl>
|
140
|
-
|
141
|
-
```
|
69
|
+
```
|
142
|
-
|
143
|
-
|
144
|
-
|
70
|
+
|
145
|
-
PrismUserControl1.xaml
|
71
|
+
```xml:PrismUserControl1.xaml
|
146
|
-
|
147
|
-
```xaml
|
148
|
-
|
149
72
|
<UserControl
|
150
|
-
|
151
73
|
x:Class="Questions362091.Views.PrismUserControl1"
|
152
|
-
|
153
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
74
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
154
|
-
|
155
75
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
156
|
-
|
157
|
-
<StackPanel>
|
76
|
+
<StackPanel>
|
158
|
-
|
159
77
|
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
160
|
-
|
161
78
|
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
162
|
-
|
163
79
|
<TextBlock Text="{Binding Output.Value}" />
|
164
|
-
|
165
|
-
</StackPanel>
|
80
|
+
</StackPanel>
|
166
|
-
|
167
81
|
</UserControl>
|
168
|
-
|
169
|
-
```
|
82
|
+
```
|
170
|
-
|
171
|
-
|
172
|
-
|
83
|
+
|
173
|
-
UserControl2.xaml
|
84
|
+
```xml:UserControl2.xaml
|
174
|
-
|
175
|
-
```xaml
|
176
|
-
|
177
85
|
<UserControl
|
178
|
-
|
179
86
|
x:Class="Questions362091.Views.UserControl2"
|
180
|
-
|
181
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
87
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
182
|
-
|
183
88
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
184
|
-
|
185
89
|
xmlns:views="clr-namespace:Questions362091.Views">
|
186
|
-
|
187
|
-
<StackPanel>
|
90
|
+
<StackPanel>
|
188
|
-
|
189
91
|
<TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type views:UserControl2}}}" />
|
190
|
-
|
191
92
|
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
192
|
-
|
193
93
|
<TextBlock Text="{Binding Output, RelativeSource={RelativeSource AncestorType={x:Type views:UserControl2}}}" />
|
194
|
-
|
195
|
-
</StackPanel>
|
94
|
+
</StackPanel>
|
196
|
-
|
197
95
|
</UserControl>
|
198
|
-
|
199
|
-
```
|
96
|
+
```
|
200
|
-
|
201
|
-
|
202
|
-
|
97
|
+
|
203
|
-
UserControl2.xaml.cs
|
98
|
+
```cs:UserControl2.xaml.cs
|
204
|
-
|
205
|
-
```C#
|
206
|
-
|
207
99
|
using System;
|
208
|
-
|
209
100
|
using System.Windows;
|
210
|
-
|
211
101
|
using System.Windows.Controls;
|
212
102
|
|
213
|
-
|
214
|
-
|
215
103
|
namespace Questions362091.Views
|
216
|
-
|
217
|
-
{
|
104
|
+
{
|
218
|
-
|
219
105
|
public partial class UserControl2 : UserControl
|
220
|
-
|
221
|
-
{
|
106
|
+
{
|
222
|
-
|
223
107
|
public string Input { get => (string)GetValue(InputProperty); set => SetValue(InputProperty, value); }
|
224
|
-
|
225
108
|
public static readonly DependencyProperty InputProperty = DependencyProperty.Register(nameof(Input), typeof(string), typeof(UserControl2), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, InputChanged));
|
226
|
-
|
227
109
|
private static void InputChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
228
|
-
|
229
|
-
{
|
110
|
+
{
|
230
|
-
|
231
111
|
if (d is UserControl2 userControl2)
|
232
|
-
|
233
112
|
userControl2.Output = userControl2.Converter?.Invoke((string)e.NewValue);
|
234
|
-
|
235
|
-
}
|
113
|
+
}
|
236
|
-
|
237
|
-
|
238
114
|
|
239
115
|
public string Output { get => (string)GetValue(OutputProperty); protected set => SetValue(OutputPropertyKey, value); }
|
240
|
-
|
241
116
|
private static readonly DependencyPropertyKey OutputPropertyKey = DependencyProperty.RegisterReadOnly(nameof(Output), typeof(string), typeof(UserControl2), new());
|
242
|
-
|
243
117
|
public static readonly DependencyProperty OutputProperty = OutputPropertyKey.DependencyProperty;
|
244
118
|
|
245
|
-
|
246
|
-
|
247
119
|
public Func<string, string> Converter { get => (Func<string, string>)GetValue(ConverterProperty); set => SetValue(ConverterProperty, value); }
|
248
|
-
|
249
120
|
public static readonly DependencyProperty ConverterProperty = DependencyProperty.Register(nameof(Converter), typeof(Func<string, string>), typeof(UserControl2), new((Func<string, string>)(s => s)));
|
250
121
|
|
251
|
-
|
252
|
-
|
253
122
|
public UserControl2() => InitializeComponent();
|
254
|
-
|
255
|
-
}
|
123
|
+
}
|
256
|
-
|
257
|
-
}
|
124
|
+
}
|
258
|
-
|
259
|
-
```
|
125
|
+
```
|
260
|
-
|
261
|
-
|
262
|
-
|
126
|
+
|
263
|
-
MainWindowViewModel.cs
|
127
|
+
```cs:MainWindowViewModel.cs
|
264
|
-
|
265
|
-
```C#
|
266
|
-
|
267
128
|
using Prism.Mvvm;
|
268
|
-
|
269
129
|
using Prism.Regions;
|
270
|
-
|
271
130
|
using Questions362091.Models;
|
272
|
-
|
273
131
|
using Reactive.Bindings;
|
274
|
-
|
275
132
|
using Reactive.Bindings.Extensions;
|
276
|
-
|
277
133
|
using System;
|
278
134
|
|
279
|
-
|
280
|
-
|
281
135
|
namespace Questions362091.ViewModels
|
282
|
-
|
283
|
-
{
|
136
|
+
{
|
284
|
-
|
285
137
|
public class MainWindowViewModel : BindableBase
|
286
|
-
|
287
|
-
{
|
138
|
+
{
|
288
|
-
|
289
139
|
public ReactiveProperty<string> Input { get; }
|
290
|
-
|
291
140
|
public ReadOnlyReactiveProperty<string> Output { get; }
|
292
|
-
|
293
141
|
public Func<string, string> Converter => model.Converter;
|
294
142
|
|
295
|
-
|
296
|
-
|
297
143
|
private readonly Model model;
|
298
144
|
|
299
|
-
|
300
|
-
|
301
145
|
public MainWindowViewModel(Model model, IRegionManager regionManager)
|
302
|
-
|
303
|
-
{
|
146
|
+
{
|
304
|
-
|
305
147
|
this.model = model;
|
306
|
-
|
307
148
|
Input = model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
|
308
|
-
|
309
149
|
Output = model.Output.ToReadOnlyReactiveProperty();
|
310
|
-
|
311
|
-
}
|
150
|
+
}
|
312
|
-
|
313
|
-
}
|
151
|
+
}
|
314
|
-
|
315
|
-
}
|
152
|
+
}
|
316
|
-
|
317
|
-
```
|
153
|
+
```
|
318
|
-
|
319
|
-
|
320
|
-
|
154
|
+
|
321
|
-
PrismUserControl1ViewModel.cs
|
155
|
+
```cs:PrismUserControl1ViewModel.cs
|
322
|
-
|
323
|
-
```C#
|
324
|
-
|
325
156
|
using Prism.Mvvm;
|
326
|
-
|
327
157
|
using Questions362091.Models;
|
328
|
-
|
329
158
|
using Reactive.Bindings;
|
330
|
-
|
331
159
|
using Reactive.Bindings.Extensions;
|
332
160
|
|
333
|
-
|
334
|
-
|
335
161
|
namespace Questions362091.ViewModels
|
336
|
-
|
337
|
-
{
|
162
|
+
{
|
338
|
-
|
339
163
|
public class PrismUserControl1ViewModel : BindableBase
|
340
|
-
|
341
|
-
{
|
164
|
+
{
|
342
|
-
|
343
165
|
public ReactiveProperty<string> Input { get; }
|
344
|
-
|
345
166
|
public ReadOnlyReactiveProperty<string> Output { get; }
|
346
167
|
|
347
|
-
|
348
|
-
|
349
168
|
public PrismUserControl1ViewModel(Model model)
|
350
|
-
|
351
|
-
{
|
169
|
+
{
|
352
|
-
|
353
170
|
Input = model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
|
354
|
-
|
355
171
|
Output = model.Output.ToReadOnlyReactiveProperty();
|
356
|
-
|
357
|
-
}
|
172
|
+
}
|
358
|
-
|
359
|
-
}
|
173
|
+
}
|
360
|
-
|
361
|
-
}
|
174
|
+
}
|
362
|
-
|
363
|
-
```
|
175
|
+
```
|
364
|
-
|
365
|
-
|
366
|
-
|
176
|
+
|
367
|
-
Model.cs
|
177
|
+
```cs:Model.cs
|
368
|
-
|
369
|
-
```C#
|
370
|
-
|
371
178
|
using Reactive.Bindings;
|
372
|
-
|
373
179
|
using System;
|
374
|
-
|
375
180
|
using System.Reactive.Linq;
|
376
181
|
|
377
|
-
|
378
|
-
|
379
182
|
namespace Questions362091.Models
|
380
|
-
|
381
|
-
{
|
183
|
+
{
|
382
|
-
|
383
184
|
public class Model
|
384
|
-
|
385
|
-
{
|
185
|
+
{
|
386
|
-
|
387
186
|
public ReactivePropertySlim<string> Input { get; } = new();
|
388
|
-
|
389
187
|
public ReadOnlyReactivePropertySlim<string> Output { get; }
|
390
|
-
|
391
188
|
public Func<string, string> Converter { get; } = s => s?.ToUpper();
|
392
189
|
|
393
|
-
|
394
|
-
|
395
190
|
public Model() => Output = Input.Select(Converter).ToReadOnlyReactivePropertySlim();
|
396
|
-
|
397
|
-
}
|
191
|
+
}
|
398
|
-
|
399
|
-
}
|
192
|
+
}
|
400
|
-
|
401
|
-
```
|
193
|
+
```
|
402
|
-
|
403
|
-
|
404
|
-
|
194
|
+
|
405
|
-
App.xaml.cs
|
195
|
+
```cs:App.xaml.cs
|
406
|
-
|
407
|
-
```C#
|
408
|
-
|
409
196
|
using Prism.Ioc;
|
410
|
-
|
411
197
|
using Prism.Regions;
|
412
|
-
|
413
198
|
using Questions362091.Models;
|
414
|
-
|
415
199
|
using Questions362091.Views;
|
416
|
-
|
417
200
|
using System.Windows;
|
418
201
|
|
419
|
-
|
420
|
-
|
421
202
|
namespace Questions362091
|
422
|
-
|
423
|
-
{
|
203
|
+
{
|
424
|
-
|
425
204
|
public partial class App
|
426
|
-
|
427
|
-
{
|
205
|
+
{
|
428
|
-
|
429
206
|
protected override Window CreateShell() => Container.Resolve<MainWindow>();
|
430
|
-
|
431
207
|
protected override void OnInitialized()
|
432
|
-
|
433
|
-
{
|
208
|
+
{
|
434
|
-
|
435
209
|
base.OnInitialized();
|
436
|
-
|
437
210
|
var regionManager = Container.Resolve<IRegionManager>();
|
438
|
-
|
439
211
|
regionManager.RegisterViewWithRegion("ContentRegion", typeof(PrismUserControl1));
|
440
|
-
|
441
|
-
}
|
212
|
+
}
|
442
|
-
|
443
213
|
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
444
|
-
|
445
|
-
{
|
214
|
+
{
|
446
|
-
|
447
215
|
containerRegistry.RegisterSingleton<Model>();
|
448
|
-
|
449
216
|
containerRegistry.RegisterForNavigation<PrismUserControl1>();
|
450
|
-
|
451
|
-
}
|
217
|
+
}
|
452
|
-
|
453
|
-
}
|
218
|
+
}
|
454
|
-
|
455
|
-
}
|
219
|
+
}
|
456
|
-
|
457
|
-
```
|
220
|
+
```
|
1
例
test
CHANGED
@@ -21,3 +21,437 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
一度Prism Full App(.NET Core)テンプレートを作っていただいて、`ViewA`・`ViewAViewModel`・`MessageService`の関係を見ていただけたらいいと思います。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
---
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
例がないと話の前提が合いませんので雑に作りました(`Func<T,R>`が気持ち悪いのは気にしないでください^^;
|
32
|
+
|
33
|
+
[NuGet Gallery | Prism.DryIoc 8.1.97](https://www.nuget.org/packages/Prism.DryIoc/8.1.97)
|
34
|
+
|
35
|
+
[NuGet Gallery | ReactiveProperty 7.12.0](https://www.nuget.org/packages/ReactiveProperty/7.12.0)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
`TextBox`と`TextBlock`の組が4つあります。どれも動作は同じです(入力値の小文字が大文字になって表示される)
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
4つのうち1つだけおかしい(異質な)ものがあります。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
MainWindow.xaml
|
48
|
+
|
49
|
+
```xaml
|
50
|
+
|
51
|
+
<Window
|
52
|
+
|
53
|
+
x:Class="Questions362091.Views.MainWindow"
|
54
|
+
|
55
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
56
|
+
|
57
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
58
|
+
|
59
|
+
xmlns:prism="http://prismlibrary.com/"
|
60
|
+
|
61
|
+
xmlns:views="clr-namespace:Questions362091.Views"
|
62
|
+
|
63
|
+
Width="525"
|
64
|
+
|
65
|
+
SizeToContent="Height">
|
66
|
+
|
67
|
+
<StackPanel>
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
<GroupBox Header="ベタ置き">
|
72
|
+
|
73
|
+
<StackPanel>
|
74
|
+
|
75
|
+
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
76
|
+
|
77
|
+
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
78
|
+
|
79
|
+
<TextBlock Text="{Binding Output.Value}" />
|
80
|
+
|
81
|
+
</StackPanel>
|
82
|
+
|
83
|
+
</GroupBox>
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
<GroupBox Header="UserControl1">
|
88
|
+
|
89
|
+
<views:UserControl1 />
|
90
|
+
|
91
|
+
</GroupBox>
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
<GroupBox Header="PrismUserControl1">
|
96
|
+
|
97
|
+
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
|
98
|
+
|
99
|
+
</GroupBox>
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
<GroupBox Header="UserControl2">
|
104
|
+
|
105
|
+
<views:UserControl2 Converter="{Binding Converter}" Input="{Binding Input.Value}" />
|
106
|
+
|
107
|
+
</GroupBox>
|
108
|
+
|
109
|
+
</StackPanel>
|
110
|
+
|
111
|
+
</Window>
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
UserControl1.xaml
|
118
|
+
|
119
|
+
```xaml
|
120
|
+
|
121
|
+
<UserControl
|
122
|
+
|
123
|
+
x:Class="Questions362091.Views.UserControl1"
|
124
|
+
|
125
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
126
|
+
|
127
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
128
|
+
|
129
|
+
<StackPanel>
|
130
|
+
|
131
|
+
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
132
|
+
|
133
|
+
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
134
|
+
|
135
|
+
<TextBlock Text="{Binding Output.Value}" />
|
136
|
+
|
137
|
+
</StackPanel>
|
138
|
+
|
139
|
+
</UserControl>
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
PrismUserControl1.xaml
|
146
|
+
|
147
|
+
```xaml
|
148
|
+
|
149
|
+
<UserControl
|
150
|
+
|
151
|
+
x:Class="Questions362091.Views.PrismUserControl1"
|
152
|
+
|
153
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
154
|
+
|
155
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
156
|
+
|
157
|
+
<StackPanel>
|
158
|
+
|
159
|
+
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
|
160
|
+
|
161
|
+
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
162
|
+
|
163
|
+
<TextBlock Text="{Binding Output.Value}" />
|
164
|
+
|
165
|
+
</StackPanel>
|
166
|
+
|
167
|
+
</UserControl>
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
UserControl2.xaml
|
174
|
+
|
175
|
+
```xaml
|
176
|
+
|
177
|
+
<UserControl
|
178
|
+
|
179
|
+
x:Class="Questions362091.Views.UserControl2"
|
180
|
+
|
181
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
182
|
+
|
183
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
184
|
+
|
185
|
+
xmlns:views="clr-namespace:Questions362091.Views">
|
186
|
+
|
187
|
+
<StackPanel>
|
188
|
+
|
189
|
+
<TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type views:UserControl2}}}" />
|
190
|
+
|
191
|
+
<TextBlock HorizontalAlignment="Center" Text="↓" />
|
192
|
+
|
193
|
+
<TextBlock Text="{Binding Output, RelativeSource={RelativeSource AncestorType={x:Type views:UserControl2}}}" />
|
194
|
+
|
195
|
+
</StackPanel>
|
196
|
+
|
197
|
+
</UserControl>
|
198
|
+
|
199
|
+
```
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
UserControl2.xaml.cs
|
204
|
+
|
205
|
+
```C#
|
206
|
+
|
207
|
+
using System;
|
208
|
+
|
209
|
+
using System.Windows;
|
210
|
+
|
211
|
+
using System.Windows.Controls;
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
namespace Questions362091.Views
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
public partial class UserControl2 : UserControl
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
public string Input { get => (string)GetValue(InputProperty); set => SetValue(InputProperty, value); }
|
224
|
+
|
225
|
+
public static readonly DependencyProperty InputProperty = DependencyProperty.Register(nameof(Input), typeof(string), typeof(UserControl2), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, InputChanged));
|
226
|
+
|
227
|
+
private static void InputChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
228
|
+
|
229
|
+
{
|
230
|
+
|
231
|
+
if (d is UserControl2 userControl2)
|
232
|
+
|
233
|
+
userControl2.Output = userControl2.Converter?.Invoke((string)e.NewValue);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
public string Output { get => (string)GetValue(OutputProperty); protected set => SetValue(OutputPropertyKey, value); }
|
240
|
+
|
241
|
+
private static readonly DependencyPropertyKey OutputPropertyKey = DependencyProperty.RegisterReadOnly(nameof(Output), typeof(string), typeof(UserControl2), new());
|
242
|
+
|
243
|
+
public static readonly DependencyProperty OutputProperty = OutputPropertyKey.DependencyProperty;
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
public Func<string, string> Converter { get => (Func<string, string>)GetValue(ConverterProperty); set => SetValue(ConverterProperty, value); }
|
248
|
+
|
249
|
+
public static readonly DependencyProperty ConverterProperty = DependencyProperty.Register(nameof(Converter), typeof(Func<string, string>), typeof(UserControl2), new((Func<string, string>)(s => s)));
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
public UserControl2() => InitializeComponent();
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
MainWindowViewModel.cs
|
264
|
+
|
265
|
+
```C#
|
266
|
+
|
267
|
+
using Prism.Mvvm;
|
268
|
+
|
269
|
+
using Prism.Regions;
|
270
|
+
|
271
|
+
using Questions362091.Models;
|
272
|
+
|
273
|
+
using Reactive.Bindings;
|
274
|
+
|
275
|
+
using Reactive.Bindings.Extensions;
|
276
|
+
|
277
|
+
using System;
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
namespace Questions362091.ViewModels
|
282
|
+
|
283
|
+
{
|
284
|
+
|
285
|
+
public class MainWindowViewModel : BindableBase
|
286
|
+
|
287
|
+
{
|
288
|
+
|
289
|
+
public ReactiveProperty<string> Input { get; }
|
290
|
+
|
291
|
+
public ReadOnlyReactiveProperty<string> Output { get; }
|
292
|
+
|
293
|
+
public Func<string, string> Converter => model.Converter;
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
private readonly Model model;
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
public MainWindowViewModel(Model model, IRegionManager regionManager)
|
302
|
+
|
303
|
+
{
|
304
|
+
|
305
|
+
this.model = model;
|
306
|
+
|
307
|
+
Input = model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
|
308
|
+
|
309
|
+
Output = model.Output.ToReadOnlyReactiveProperty();
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
```
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
PrismUserControl1ViewModel.cs
|
322
|
+
|
323
|
+
```C#
|
324
|
+
|
325
|
+
using Prism.Mvvm;
|
326
|
+
|
327
|
+
using Questions362091.Models;
|
328
|
+
|
329
|
+
using Reactive.Bindings;
|
330
|
+
|
331
|
+
using Reactive.Bindings.Extensions;
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
namespace Questions362091.ViewModels
|
336
|
+
|
337
|
+
{
|
338
|
+
|
339
|
+
public class PrismUserControl1ViewModel : BindableBase
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
public ReactiveProperty<string> Input { get; }
|
344
|
+
|
345
|
+
public ReadOnlyReactiveProperty<string> Output { get; }
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
public PrismUserControl1ViewModel(Model model)
|
350
|
+
|
351
|
+
{
|
352
|
+
|
353
|
+
Input = model.Input.ToReactivePropertyAsSynchronized(x => x.Value);
|
354
|
+
|
355
|
+
Output = model.Output.ToReadOnlyReactiveProperty();
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
```
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
Model.cs
|
368
|
+
|
369
|
+
```C#
|
370
|
+
|
371
|
+
using Reactive.Bindings;
|
372
|
+
|
373
|
+
using System;
|
374
|
+
|
375
|
+
using System.Reactive.Linq;
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
namespace Questions362091.Models
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
public class Model
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
public ReactivePropertySlim<string> Input { get; } = new();
|
388
|
+
|
389
|
+
public ReadOnlyReactivePropertySlim<string> Output { get; }
|
390
|
+
|
391
|
+
public Func<string, string> Converter { get; } = s => s?.ToUpper();
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
public Model() => Output = Input.Select(Converter).ToReadOnlyReactivePropertySlim();
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
```
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
App.xaml.cs
|
406
|
+
|
407
|
+
```C#
|
408
|
+
|
409
|
+
using Prism.Ioc;
|
410
|
+
|
411
|
+
using Prism.Regions;
|
412
|
+
|
413
|
+
using Questions362091.Models;
|
414
|
+
|
415
|
+
using Questions362091.Views;
|
416
|
+
|
417
|
+
using System.Windows;
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
namespace Questions362091
|
422
|
+
|
423
|
+
{
|
424
|
+
|
425
|
+
public partial class App
|
426
|
+
|
427
|
+
{
|
428
|
+
|
429
|
+
protected override Window CreateShell() => Container.Resolve<MainWindow>();
|
430
|
+
|
431
|
+
protected override void OnInitialized()
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
base.OnInitialized();
|
436
|
+
|
437
|
+
var regionManager = Container.Resolve<IRegionManager>();
|
438
|
+
|
439
|
+
regionManager.RegisterViewWithRegion("ContentRegion", typeof(PrismUserControl1));
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
444
|
+
|
445
|
+
{
|
446
|
+
|
447
|
+
containerRegistry.RegisterSingleton<Model>();
|
448
|
+
|
449
|
+
containerRegistry.RegisterForNavigation<PrismUserControl1>();
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
```
|