回答編集履歴
2
書き直し
test
CHANGED
@@ -23,9 +23,18 @@
|
|
23
23
|
|
24
24
|
---
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
`MainViewModel`を`RegisterSingleton`すれば上記で十分です。
|
27
|
+
|
28
|
+
シングルトンにしたくない場合は、`TimerTrigger`に変更したら取れました(ぶっちゃけクソっぽいが^^;
|
29
|
+
|
30
|
+
```xml
|
31
|
+
<i:TimerTrigger EventName="Loaded" MillisecondsPerTick="100" TotalTicks="1">
|
32
|
+
<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding DataContext, ElementName=MainView}" />
|
33
|
+
</i:TimerTrigger>
|
34
|
+
```
|
35
|
+
100の根拠はありません(手元では1でも0でも!! 取れました)
|
36
|
+
[TimerTrigger クラス (Microsoft.Expression.Interactivity.Core) | Microsoft Learn](https://learn.microsoft.com/ja-jp/previous-versions/visualstudio/design-tools/expression-studio-4/ff726469(v=expression.40))
|
37
|
+
|
29
38
|
|
30
39
|
```xml:MainWindow.xaml
|
31
40
|
<Window
|
@@ -33,21 +42,20 @@
|
|
33
42
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
34
43
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
35
44
|
xmlns:prism="http://prismlibrary.com/"
|
36
|
-
Title="{Binding Title}"
|
37
45
|
Width="525"
|
38
46
|
Height="350"
|
39
47
|
prism:ViewModelLocator.AutoWireViewModel="True">
|
40
48
|
<DockPanel>
|
41
49
|
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
|
42
50
|
<Button Command="{Binding NavigateCommand}" CommandParameter="ViewA">Navigate ViewA</Button>
|
43
|
-
<Button Command="{Binding NavigateCommand}" CommandParameter="Main
|
51
|
+
<Button Command="{Binding NavigateCommand}" CommandParameter="Main">Navigate Main</Button>
|
44
|
-
<Button Command="{Binding ShowDialogCommand}">Show Main
|
52
|
+
<Button Command="{Binding ShowDialogCommand}">Show MainDialog</Button>
|
45
53
|
</StackPanel>
|
46
54
|
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
|
47
55
|
</DockPanel>
|
48
56
|
</Window>
|
49
57
|
```
|
50
|
-
```cs:MainWindow
|
58
|
+
```cs:MainWindowViewModel.cs
|
51
59
|
using Prism.Mvvm;
|
52
60
|
using Prism.Regions;
|
53
61
|
using Prism.Services.Dialogs;
|
@@ -58,39 +66,31 @@
|
|
58
66
|
|
59
67
|
public class MainWindowViewModel : BindableBase
|
60
68
|
{
|
61
|
-
private string _title = "Prism Application";
|
62
|
-
public string Title { get => _title; set => SetProperty(ref _title, value); }
|
63
|
-
|
64
69
|
public ReactiveCommand<string> NavigateCommand { get; }
|
65
70
|
public ReactiveCommand ShowDialogCommand { get; }
|
66
71
|
|
67
|
-
private IRegionManager _regionManager;
|
68
|
-
private IDialogService _dialogService;
|
69
|
-
|
70
72
|
public MainWindowViewModel(IRegionManager regionManager, IDialogService dialogService)
|
71
73
|
{
|
72
|
-
_regionManager = regionManager;
|
73
|
-
_dialogService = dialogService;
|
74
|
-
|
75
74
|
NavigateCommand = new ReactiveCommand<string>()
|
76
|
-
.WithSubscribe(x =>
|
75
|
+
.WithSubscribe(x => regionManager.RequestNavigate("ContentRegion", x));
|
77
76
|
|
78
77
|
ShowDialogCommand = new ReactiveCommand()
|
79
|
-
.WithSubscribe(() =>
|
78
|
+
.WithSubscribe(() => dialogService.ShowDialog(nameof(Main)));
|
80
|
-
}
|
79
|
+
}
|
81
|
-
}
|
80
|
+
}
|
82
|
-
```
|
81
|
+
```
|
83
|
-
|
82
|
+
|
84
|
-
```xml:Main
|
83
|
+
```xml:Main.xaml
|
85
84
|
<UserControl
|
86
|
-
x:Class="Q07l0vpm2ryc05t.Views.Main
|
85
|
+
x:Class="Q07l0vpm2ryc05t.Views.Main"
|
87
86
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
88
87
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
89
88
|
xmlns:TestArea="clr-namespace:Q07l0vpm2ryc05t.Views"
|
90
89
|
xmlns:prism="http://prismlibrary.com/"
|
90
|
+
x:Name="MainView"
|
91
91
|
prism:ViewModelLocator.AutoWireViewModel="True">
|
92
92
|
<StackPanel>
|
93
|
-
<TextBlock Text="Main
|
93
|
+
<TextBlock Text="Main" />
|
94
94
|
<TextBlock Text="{Binding HashCode}" />
|
95
95
|
<GroupBox Margin="5">
|
96
96
|
<TestArea:ChildArea />
|
@@ -98,7 +98,7 @@
|
|
98
98
|
</StackPanel>
|
99
99
|
</UserControl>
|
100
100
|
```
|
101
|
-
```cs:MainView
|
101
|
+
```cs:MainViewModel.cs
|
102
102
|
using System;
|
103
103
|
using Prism.Mvvm;
|
104
104
|
using Prism.Services.Dialogs;
|
@@ -108,8 +108,8 @@
|
|
108
108
|
public class MainViewModel : BindableBase, IDialogAware
|
109
109
|
{
|
110
110
|
public string HashCode => $"MainViewModel.HashCode:{GetHashCode()}";
|
111
|
+
|
111
|
-
public string Title => "Main
|
112
|
+
public string Title => "MainDialog";
|
112
|
-
|
113
113
|
public event Action<IDialogResult> RequestClose;
|
114
114
|
|
115
115
|
public MainViewModel() { }
|
@@ -125,25 +125,57 @@
|
|
125
125
|
x:Class="Q07l0vpm2ryc05t.Views.ChildArea"
|
126
126
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
127
127
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
128
|
+
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
|
128
129
|
xmlns:prism="http://prismlibrary.com/"
|
130
|
+
x:Name="ChildAreaView"
|
129
131
|
prism:ViewModelLocator.AutoWireViewModel="True">
|
132
|
+
<i:Interaction.Triggers>
|
133
|
+
|
134
|
+
<!--<i:EventTrigger EventName="Loaded">
|
135
|
+
<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding DataContext, ElementName=MainView}" />
|
136
|
+
</i:EventTrigger>-->
|
137
|
+
|
138
|
+
<i:TimerTrigger
|
139
|
+
EventName="Loaded"
|
140
|
+
MillisecondsPerTick="0"
|
141
|
+
TotalTicks="1">
|
142
|
+
<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding DataContext, ElementName=MainView}" />
|
143
|
+
</i:TimerTrigger>
|
144
|
+
|
145
|
+
</i:Interaction.Triggers>
|
146
|
+
|
130
147
|
<StackPanel>
|
131
148
|
<TextBlock Text="ChildArea" />
|
132
149
|
<TextBlock Text="{Binding HashCode}" />
|
133
150
|
</StackPanel>
|
134
151
|
</UserControl>
|
135
152
|
```
|
136
|
-
```cs:ChildArea
|
153
|
+
```cs:ChildAreaViewModel.cs
|
137
154
|
using Prism.Mvvm;
|
155
|
+
using Reactive.Bindings;
|
138
156
|
|
139
157
|
namespace Q07l0vpm2ryc05t.ViewModels;
|
140
158
|
|
141
159
|
public class ChildAreaViewModel : BindableBase
|
142
160
|
{
|
143
|
-
public string HashCode => $"
|
161
|
+
public string HashCode => $"MainViewModel.HashCode:{_mainViewModel?.GetHashCode()}";
|
162
|
+
public ReactiveCommand<object> LoadedCommand { get; }
|
144
163
|
|
145
164
|
private MainViewModel _mainViewModel;
|
165
|
+
|
146
|
-
public ChildAreaViewModel(MainViewModel mainViewModel)
|
166
|
+
public ChildAreaViewModel(MainViewModel mainViewModel)
|
167
|
+
{
|
168
|
+
// RegisterSingletonでいいならこれだけ
|
169
|
+
//_mainViewModel = mainViewModel;
|
170
|
+
|
171
|
+
LoadedCommand = new ReactiveCommand<object>().WithSubscribe(Loaded);
|
172
|
+
}
|
173
|
+
|
174
|
+
private void Loaded(object mainViewModel)
|
175
|
+
{
|
176
|
+
_mainViewModel = mainViewModel as MainViewModel;
|
177
|
+
RaisePropertyChanged(nameof(HashCode));
|
178
|
+
}
|
147
179
|
}
|
148
180
|
```
|
149
181
|
|
@@ -161,13 +193,13 @@
|
|
161
193
|
|
162
194
|
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
163
195
|
{
|
164
|
-
containerRegistry.RegisterSingleton<MainViewModel>();
|
196
|
+
//containerRegistry.RegisterSingleton<MainViewModel>();
|
165
197
|
|
166
198
|
containerRegistry.RegisterForNavigation<ViewA>();
|
167
|
-
containerRegistry.RegisterForNavigation<Main
|
199
|
+
containerRegistry.RegisterForNavigation<Main>();
|
168
|
-
|
200
|
+
|
169
|
-
containerRegistry.RegisterDialog<Main
|
201
|
+
containerRegistry.RegisterDialog<Main>();
|
170
|
-
}
|
202
|
+
}
|
171
|
-
}
|
203
|
+
}
|
172
|
-
```
|
204
|
+
```
|
173
|
-
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-07-22/2
|
205
|
+
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-07-22/0d0a2ab2-0c06-4480-a596-218263aec7ce.gif)
|
1
追記
test
CHANGED
@@ -20,3 +20,154 @@
|
|
20
20
|
> そうすると、ChildViewModelのLoadedコマンドが呼ばれなくなりました…。
|
21
21
|
|
22
22
|
`DataContext`はひとつしか持てないので、`AutoWireViewModel`しているViewに違う`DataContext`を設定したら当然動かなくなりますよね。
|
23
|
+
|
24
|
+
---
|
25
|
+
|
26
|
+
> MainViewModelをシングルトン設定にしてもうまくいきませんでした・・・。
|
27
|
+
|
28
|
+
こちらの手元では`RegisterSingleton`すれば同一インスタンスになりましたが、何か前提が違いますか?
|
29
|
+
|
30
|
+
```xml:MainWindow.xaml
|
31
|
+
<Window
|
32
|
+
x:Class="Q07l0vpm2ryc05t.Views.MainWindow"
|
33
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
34
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
35
|
+
xmlns:prism="http://prismlibrary.com/"
|
36
|
+
Title="{Binding Title}"
|
37
|
+
Width="525"
|
38
|
+
Height="350"
|
39
|
+
prism:ViewModelLocator.AutoWireViewModel="True">
|
40
|
+
<DockPanel>
|
41
|
+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
|
42
|
+
<Button Command="{Binding NavigateCommand}" CommandParameter="ViewA">Navigate ViewA</Button>
|
43
|
+
<Button Command="{Binding NavigateCommand}" CommandParameter="MainView">Navigate MainView</Button>
|
44
|
+
<Button Command="{Binding ShowDialogCommand}">Show MainView Dialog</Button>
|
45
|
+
</StackPanel>
|
46
|
+
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
|
47
|
+
</DockPanel>
|
48
|
+
</Window>
|
49
|
+
```
|
50
|
+
```cs:MainWindow.xaml.cs
|
51
|
+
using Prism.Mvvm;
|
52
|
+
using Prism.Regions;
|
53
|
+
using Prism.Services.Dialogs;
|
54
|
+
using Q07l0vpm2ryc05t.Views;
|
55
|
+
using Reactive.Bindings;
|
56
|
+
|
57
|
+
namespace Q07l0vpm2ryc05t.ViewModels;
|
58
|
+
|
59
|
+
public class MainWindowViewModel : BindableBase
|
60
|
+
{
|
61
|
+
private string _title = "Prism Application";
|
62
|
+
public string Title { get => _title; set => SetProperty(ref _title, value); }
|
63
|
+
|
64
|
+
public ReactiveCommand<string> NavigateCommand { get; }
|
65
|
+
public ReactiveCommand ShowDialogCommand { get; }
|
66
|
+
|
67
|
+
private IRegionManager _regionManager;
|
68
|
+
private IDialogService _dialogService;
|
69
|
+
|
70
|
+
public MainWindowViewModel(IRegionManager regionManager, IDialogService dialogService)
|
71
|
+
{
|
72
|
+
_regionManager = regionManager;
|
73
|
+
_dialogService = dialogService;
|
74
|
+
|
75
|
+
NavigateCommand = new ReactiveCommand<string>()
|
76
|
+
.WithSubscribe(x => _regionManager.RequestNavigate("ContentRegion", x));
|
77
|
+
|
78
|
+
ShowDialogCommand = new ReactiveCommand()
|
79
|
+
.WithSubscribe(() => _dialogService.ShowDialog(nameof(MainView)));
|
80
|
+
}
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
```xml:MainView.xaml
|
85
|
+
<UserControl
|
86
|
+
x:Class="Q07l0vpm2ryc05t.Views.MainView"
|
87
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
88
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
89
|
+
xmlns:TestArea="clr-namespace:Q07l0vpm2ryc05t.Views"
|
90
|
+
xmlns:prism="http://prismlibrary.com/"
|
91
|
+
prism:ViewModelLocator.AutoWireViewModel="True">
|
92
|
+
<StackPanel>
|
93
|
+
<TextBlock Text="MainView" />
|
94
|
+
<TextBlock Text="{Binding HashCode}" />
|
95
|
+
<GroupBox Margin="5">
|
96
|
+
<TestArea:ChildArea />
|
97
|
+
</GroupBox>
|
98
|
+
</StackPanel>
|
99
|
+
</UserControl>
|
100
|
+
```
|
101
|
+
```cs:MainView.xaml.cs
|
102
|
+
using System;
|
103
|
+
using Prism.Mvvm;
|
104
|
+
using Prism.Services.Dialogs;
|
105
|
+
|
106
|
+
namespace Q07l0vpm2ryc05t.ViewModels;
|
107
|
+
|
108
|
+
public class MainViewModel : BindableBase, IDialogAware
|
109
|
+
{
|
110
|
+
public string HashCode => $"MainViewModel.HashCode:{GetHashCode()}";
|
111
|
+
public string Title => "MainView";
|
112
|
+
|
113
|
+
public event Action<IDialogResult> RequestClose;
|
114
|
+
|
115
|
+
public MainViewModel() { }
|
116
|
+
|
117
|
+
public bool CanCloseDialog() => true;
|
118
|
+
public void OnDialogClosed() { }
|
119
|
+
public void OnDialogOpened(IDialogParameters parameters) { }
|
120
|
+
}
|
121
|
+
```
|
122
|
+
|
123
|
+
```xml:ChildArea.xaml
|
124
|
+
<UserControl
|
125
|
+
x:Class="Q07l0vpm2ryc05t.Views.ChildArea"
|
126
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
127
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
128
|
+
xmlns:prism="http://prismlibrary.com/"
|
129
|
+
prism:ViewModelLocator.AutoWireViewModel="True">
|
130
|
+
<StackPanel>
|
131
|
+
<TextBlock Text="ChildArea" />
|
132
|
+
<TextBlock Text="{Binding HashCode}" />
|
133
|
+
</StackPanel>
|
134
|
+
</UserControl>
|
135
|
+
```
|
136
|
+
```cs:ChildArea.xaml.cs
|
137
|
+
using Prism.Mvvm;
|
138
|
+
|
139
|
+
namespace Q07l0vpm2ryc05t.ViewModels;
|
140
|
+
|
141
|
+
public class ChildAreaViewModel : BindableBase
|
142
|
+
{
|
143
|
+
public string HashCode => $"Inject MainViewModel.HashCode:{_mainViewModel.GetHashCode()}";
|
144
|
+
|
145
|
+
private MainViewModel _mainViewModel;
|
146
|
+
public ChildAreaViewModel(MainViewModel mainViewModel) => _mainViewModel = mainViewModel;
|
147
|
+
}
|
148
|
+
```
|
149
|
+
|
150
|
+
```cs:App.xaml.cs
|
151
|
+
using System.Windows;
|
152
|
+
using Prism.Ioc;
|
153
|
+
using Q07l0vpm2ryc05t.ViewModels;
|
154
|
+
using Q07l0vpm2ryc05t.Views;
|
155
|
+
|
156
|
+
namespace Q07l0vpm2ryc05t;
|
157
|
+
|
158
|
+
public partial class App
|
159
|
+
{
|
160
|
+
protected override Window CreateShell() => Container.Resolve<MainWindow>();
|
161
|
+
|
162
|
+
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
163
|
+
{
|
164
|
+
containerRegistry.RegisterSingleton<MainViewModel>();
|
165
|
+
|
166
|
+
containerRegistry.RegisterForNavigation<ViewA>();
|
167
|
+
containerRegistry.RegisterForNavigation<MainView>();
|
168
|
+
|
169
|
+
containerRegistry.RegisterDialog<MainView>();
|
170
|
+
}
|
171
|
+
}
|
172
|
+
```
|
173
|
+
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-07-22/24b3131f-5b9a-4c43-a566-fa6fa34b3c6f.gif)
|