回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,153 +1,153 @@
|
|
1
|
-
> 個人的にはAutoWireしなければ親のDataContextを引き継ぎますから、どうにでもコントロールできていいと思うのですが(Prism的にはいまいちなんですかね)
|
2
|
-
>
|
3
|
-
> あとd:DataContextにデザイン用VMなりVMでデザイン時を判断しデータを入れるようにしておくと気が利くなって思います。
|
4
|
-
|
5
|
-
回答の私のコメントを回収しておきます。
|
6
|
-
VMの条件によっては`AutoWire`してもいいと思います。
|
7
|
-
`SetDefaultViewTypeToViewModelTypeResolver`が面倒だったら、本体の`ViewAViewModel`をカラ継承しておいておくとか?^^;
|
8
|
-
|
9
|
-
本体はプラグインのことを知らないが、プラグインは本体のことを知っていていいんですよね?
|
10
|
-
|
11
|
-
|
12
|
-
本体側
|
13
|
-
```
|
14
|
-
<Window
|
15
|
-
x:Class="BlankCoreApp1.Views.MainWindow"
|
16
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
-
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
|
19
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
20
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
21
|
-
xmlns:prism="http://prismlibrary.com/"
|
22
|
-
Width="525"
|
23
|
-
Height="350"
|
24
|
-
d:DataContext="{d:DesignInstance {x:Type ViewModels:MainWindowViewModel}}"
|
25
|
-
prism:ViewModelLocator.AutoWireViewModel="True"
|
26
|
-
mc:Ignorable="d">
|
27
|
-
<Grid>
|
28
|
-
<GroupBox Header="ViewA">
|
29
|
-
<ContentControl prism:RegionManager.RegionName="ContentRegion" DataContext="{Binding ViewAViewModel}" />
|
30
|
-
</GroupBox>
|
31
|
-
</Grid>
|
32
|
-
</Window>
|
33
|
-
```
|
34
|
-
|
35
|
-
```
|
36
|
-
<UserControl
|
37
|
-
x:Class="BlankCoreApp1.Views.DefaultViewA"
|
38
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
39
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
40
|
-
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
|
41
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
42
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
43
|
-
d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
|
44
|
-
mc:Ignorable="d">
|
45
|
-
<DockPanel>
|
46
|
-
<TextBlock DockPanel.Dock="Top" Text="BlankCoreApp1.Views.DefaultViewA" />
|
47
|
-
<TextBlock Text="{Binding Message}" />
|
48
|
-
</DockPanel>
|
49
|
-
</UserControl>
|
50
|
-
```
|
51
|
-
|
52
|
-
```
|
53
|
-
using BlankCoreApp1.Views;
|
54
|
-
using Prism.Mvvm;
|
55
|
-
using Prism.Regions;
|
56
|
-
|
57
|
-
namespace BlankCoreApp1.ViewModels
|
58
|
-
{
|
59
|
-
public class MainWindowViewModel : BindableBase
|
60
|
-
{
|
61
|
-
// 本題でないので直接newするが好きにDIしてください
|
62
|
-
public ViewAViewModel ViewAViewModel { get; } = new ViewAViewModel();
|
63
|
-
|
64
|
-
public MainWindowViewModel() { }
|
65
|
-
|
66
|
-
public MainWindowViewModel(IRegionManager regionManager)
|
67
|
-
{
|
68
|
-
// CustomViewがなければデフォルトを表示
|
69
|
-
regionManager.RegisterViewWithRegion("ContentRegion", typeof(DefaultViewA));
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
public class ViewAViewModel : BindableBase
|
74
|
-
{
|
75
|
-
private string _message;
|
76
|
-
public string Message { get => _message; set => SetProperty(ref _message, value); }
|
77
|
-
|
78
|
-
public ViewAViewModel() => Message = $"{GetType().FullName}";
|
79
|
-
}
|
80
|
-
}
|
81
|
-
```
|
82
|
-
|
83
|
-
```
|
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()
|
96
|
-
{
|
97
|
-
// 指定フォルダからモジュール読み込み
|
98
|
-
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
|
99
|
-
}
|
100
|
-
|
101
|
-
protected override void RegisterTypes(IContainerRegistry containerRegistry) { }
|
102
|
-
}
|
103
|
-
}
|
104
|
-
```
|
105
|
-
|
106
|
-
---
|
107
|
-
|
108
|
-
プラグイン側
|
109
|
-
```
|
110
|
-
<UserControl
|
111
|
-
x:Class="CustomViewAModule.Views.ViewA"
|
112
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
113
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
114
|
-
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels;assembly=BlankCoreApp1"
|
115
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
116
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
117
|
-
d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
|
118
|
-
mc:Ignorable="d">
|
119
|
-
<DockPanel>
|
120
|
-
<TextBlock DockPanel.Dock="Top" Text="CustomViewAModule.Views.ViewA" />
|
121
|
-
<TextBlock Text="{Binding Message}" />
|
122
|
-
</DockPanel>
|
123
|
-
</UserControl>
|
124
|
-
```
|
125
|
-
|
126
|
-
```
|
127
|
-
using CustomViewAModule.Views;
|
128
|
-
using Prism.Ioc;
|
129
|
-
using Prism.Modularity;
|
130
|
-
using Prism.Regions;
|
131
|
-
|
132
|
-
namespace CustomViewAModule
|
133
|
-
{
|
134
|
-
public class CustomViewAModuleModule : IModule
|
135
|
-
{
|
136
|
-
public void OnInitialized(IContainerProvider containerProvider)
|
137
|
-
{
|
138
|
-
var regionManager = containerProvider.Resolve<IRegionManager>();
|
139
|
-
|
140
|
-
// CustomViewで上書きナビゲート
|
141
|
-
regionManager.RequestNavigate("ContentRegion", "ViewA");
|
142
|
-
|
143
|
-
// こちらが後に呼ばれているのにうまく出なかった
|
144
|
-
//regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
|
145
|
-
}
|
146
|
-
|
147
|
-
public void RegisterTypes(IContainerRegistry containerRegistry)
|
148
|
-
=> containerRegistry.RegisterForNavigation<ViewA>();
|
149
|
-
}
|
150
|
-
}
|
151
|
-
```
|
152
|
-
|
1
|
+
> 個人的にはAutoWireしなければ親のDataContextを引き継ぎますから、どうにでもコントロールできていいと思うのですが(Prism的にはいまいちなんですかね)
|
2
|
+
>
|
3
|
+
> あとd:DataContextにデザイン用VMなりVMでデザイン時を判断しデータを入れるようにしておくと気が利くなって思います。
|
4
|
+
|
5
|
+
回答の私のコメントを回収しておきます。
|
6
|
+
VMの条件によっては`AutoWire`してもいいと思います。
|
7
|
+
`SetDefaultViewTypeToViewModelTypeResolver`が面倒だったら、本体の`ViewAViewModel`をカラ継承しておいておくとか?^^;
|
8
|
+
|
9
|
+
本体はプラグインのことを知らないが、プラグインは本体のことを知っていていいんですよね?
|
10
|
+
|
11
|
+
|
12
|
+
本体側
|
13
|
+
```xml
|
14
|
+
<Window
|
15
|
+
x:Class="BlankCoreApp1.Views.MainWindow"
|
16
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
+
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
|
19
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
20
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
21
|
+
xmlns:prism="http://prismlibrary.com/"
|
22
|
+
Width="525"
|
23
|
+
Height="350"
|
24
|
+
d:DataContext="{d:DesignInstance {x:Type ViewModels:MainWindowViewModel}}"
|
25
|
+
prism:ViewModelLocator.AutoWireViewModel="True"
|
26
|
+
mc:Ignorable="d">
|
27
|
+
<Grid>
|
28
|
+
<GroupBox Header="ViewA">
|
29
|
+
<ContentControl prism:RegionManager.RegionName="ContentRegion" DataContext="{Binding ViewAViewModel}" />
|
30
|
+
</GroupBox>
|
31
|
+
</Grid>
|
32
|
+
</Window>
|
33
|
+
```
|
34
|
+
|
35
|
+
```xml
|
36
|
+
<UserControl
|
37
|
+
x:Class="BlankCoreApp1.Views.DefaultViewA"
|
38
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
39
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
40
|
+
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels"
|
41
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
42
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
43
|
+
d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
|
44
|
+
mc:Ignorable="d">
|
45
|
+
<DockPanel>
|
46
|
+
<TextBlock DockPanel.Dock="Top" Text="BlankCoreApp1.Views.DefaultViewA" />
|
47
|
+
<TextBlock Text="{Binding Message}" />
|
48
|
+
</DockPanel>
|
49
|
+
</UserControl>
|
50
|
+
```
|
51
|
+
|
52
|
+
```cs
|
53
|
+
using BlankCoreApp1.Views;
|
54
|
+
using Prism.Mvvm;
|
55
|
+
using Prism.Regions;
|
56
|
+
|
57
|
+
namespace BlankCoreApp1.ViewModels
|
58
|
+
{
|
59
|
+
public class MainWindowViewModel : BindableBase
|
60
|
+
{
|
61
|
+
// 本題でないので直接newするが好きにDIしてください
|
62
|
+
public ViewAViewModel ViewAViewModel { get; } = new ViewAViewModel();
|
63
|
+
|
64
|
+
public MainWindowViewModel() { }
|
65
|
+
|
66
|
+
public MainWindowViewModel(IRegionManager regionManager)
|
67
|
+
{
|
68
|
+
// CustomViewがなければデフォルトを表示
|
69
|
+
regionManager.RegisterViewWithRegion("ContentRegion", typeof(DefaultViewA));
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
public class ViewAViewModel : BindableBase
|
74
|
+
{
|
75
|
+
private string _message;
|
76
|
+
public string Message { get => _message; set => SetProperty(ref _message, value); }
|
77
|
+
|
78
|
+
public ViewAViewModel() => Message = $"{GetType().FullName}";
|
79
|
+
}
|
80
|
+
}
|
81
|
+
```
|
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()
|
96
|
+
{
|
97
|
+
// 指定フォルダからモジュール読み込み
|
98
|
+
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
|
99
|
+
}
|
100
|
+
|
101
|
+
protected override void RegisterTypes(IContainerRegistry containerRegistry) { }
|
102
|
+
}
|
103
|
+
}
|
104
|
+
```
|
105
|
+
|
106
|
+
---
|
107
|
+
|
108
|
+
プラグイン側
|
109
|
+
```xml
|
110
|
+
<UserControl
|
111
|
+
x:Class="CustomViewAModule.Views.ViewA"
|
112
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
113
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
114
|
+
xmlns:ViewModels="clr-namespace:BlankCoreApp1.ViewModels;assembly=BlankCoreApp1"
|
115
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
116
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
117
|
+
d:DataContext="{d:DesignInstance {x:Type ViewModels:ViewAViewModel}, IsDesignTimeCreatable=True}"
|
118
|
+
mc:Ignorable="d">
|
119
|
+
<DockPanel>
|
120
|
+
<TextBlock DockPanel.Dock="Top" Text="CustomViewAModule.Views.ViewA" />
|
121
|
+
<TextBlock Text="{Binding Message}" />
|
122
|
+
</DockPanel>
|
123
|
+
</UserControl>
|
124
|
+
```
|
125
|
+
|
126
|
+
```cs
|
127
|
+
using CustomViewAModule.Views;
|
128
|
+
using Prism.Ioc;
|
129
|
+
using Prism.Modularity;
|
130
|
+
using Prism.Regions;
|
131
|
+
|
132
|
+
namespace CustomViewAModule
|
133
|
+
{
|
134
|
+
public class CustomViewAModuleModule : IModule
|
135
|
+
{
|
136
|
+
public void OnInitialized(IContainerProvider containerProvider)
|
137
|
+
{
|
138
|
+
var regionManager = containerProvider.Resolve<IRegionManager>();
|
139
|
+
|
140
|
+
// CustomViewで上書きナビゲート
|
141
|
+
regionManager.RequestNavigate("ContentRegion", "ViewA");
|
142
|
+
|
143
|
+
// こちらが後に呼ばれているのにうまく出なかった
|
144
|
+
//regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
|
145
|
+
}
|
146
|
+
|
147
|
+
public void RegisterTypes(IContainerRegistry containerRegistry)
|
148
|
+
=> containerRegistry.RegisterForNavigation<ViewA>();
|
149
|
+
}
|
150
|
+
}
|
151
|
+
```
|
152
|
+
|
153
153
|

|