回答編集履歴
1
追記
test
CHANGED
@@ -100,7 +100,115 @@
|
|
100
100
|
[IProgress<T> Interface (System) | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/api/system.iprogress-1)
|
101
101
|
[wpf iprogress - Google 検索](https://www.google.co.jp/search?q=wpf+iprogress)
|
102
102
|
|
103
|
-
|
103
|
+
---
|
104
|
+
|
104
|
-
|
105
|
+
> そのあたりを一切変えないとすると、MainWindowを隠しておく位しか対処はない気がします。
|
106
|
+
|
105
|
-
|
107
|
+
```xml
|
106
|
-
|
108
|
+
<Window
|
109
|
+
x:Class="Q4mn6unrgvjhe45.MainWindow"
|
110
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
111
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
112
|
+
Title="MainWindow"
|
113
|
+
Width="800"
|
114
|
+
Height="450">
|
115
|
+
<Window.Resources>
|
116
|
+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
|
117
|
+
</Window.Resources>
|
118
|
+
<Window.Visibility>
|
119
|
+
<Binding
|
120
|
+
Converter="{StaticResource BooleanToVisibilityConverter}"
|
121
|
+
Mode="TwoWay"
|
122
|
+
Path="IsVisible" />
|
123
|
+
</Window.Visibility>
|
124
|
+
</Window>
|
125
|
+
```
|
126
|
+
```cs
|
127
|
+
using System.Windows;
|
128
|
+
using System.Windows.Data;
|
129
|
+
using CommunityToolkit.Mvvm.ComponentModel;
|
130
|
+
|
131
|
+
namespace Q4mn6unrgvjhe45;
|
132
|
+
|
133
|
+
public partial class MainWindow : Window
|
134
|
+
{
|
135
|
+
public MainWindow()
|
136
|
+
{
|
137
|
+
InitializeComponent();
|
138
|
+
Visibility = Visibility.Hidden; // バインドだけで済ませたいがチラッと見えちゃうので^^;
|
139
|
+
DataContext = new MainViewModel();
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
partial class MainViewModel : ObservableObject
|
144
|
+
{
|
145
|
+
[ObservableProperty] private int value;
|
146
|
+
[ObservableProperty] private bool isVisible;
|
147
|
+
|
148
|
+
public MainViewModel()
|
149
|
+
{
|
150
|
+
XXXWindow xWin = new XXXWindow();
|
151
|
+
Binding binding = new Binding(nameof(Value)) { Source = this, };
|
152
|
+
xWin.SetBinding(XXXWindow.ValueProperty, binding);
|
153
|
+
xWin.Show();
|
154
|
+
|
155
|
+
// 別スレッドで処理(投げっぱなし)
|
156
|
+
Task.Run(() =>
|
157
|
+
{
|
158
|
+
Thread.Sleep(1000); // 何か重い処理
|
159
|
+
Value = 50;
|
160
|
+
Thread.Sleep(1000); // 何か重い処理
|
161
|
+
Value = 100;
|
162
|
+
|
163
|
+
Thread.Sleep(500); // 100%見えてから
|
164
|
+
Application.Current.Dispatcher.Invoke(() => xWin.Close());
|
165
|
+
IsVisible = true;
|
166
|
+
});
|
167
|
+
}
|
168
|
+
}
|
169
|
+
```
|
170
|
+
|
171
|
+
---
|
172
|
+
|
173
|
+
> 重い処理を書く場所を変えていいなら、App.xaml.csでやるほうが素直な気はします。
|
174
|
+
|
175
|
+
```xml
|
176
|
+
<Application
|
177
|
+
x:Class="Q4mn6unrgvjhe45.App"
|
178
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
179
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
180
|
+
xmlns:local="clr-namespace:Q4mn6unrgvjhe45"
|
181
|
+
Startup="Application_Startup" />
|
182
|
+
```
|
183
|
+
```cs
|
184
|
+
using System.Windows;
|
185
|
+
|
186
|
+
namespace Q4mn6unrgvjhe45;
|
187
|
+
|
188
|
+
public partial class App : Application
|
189
|
+
{
|
190
|
+
private async void Application_Startup(object sender, StartupEventArgs e)
|
191
|
+
{
|
192
|
+
MainWindow MainWindow = new MainWindow();
|
193
|
+
|
194
|
+
XXXWindow xWin = new XXXWindow();
|
195
|
+
xWin.Value = 0;
|
196
|
+
xWin.Show();
|
197
|
+
|
198
|
+
// 別スレッドでの処理を待機
|
199
|
+
await Task.Run(() =>
|
200
|
+
{
|
201
|
+
Thread.Sleep(1000); // 何か重い処理
|
202
|
+
Current.Dispatcher.Invoke(() => xWin.Value = 50); // UIスレッドで更新
|
203
|
+
|
204
|
+
Thread.Sleep(1000); // 何か重い処理
|
205
|
+
Current.Dispatcher.Invoke(() => xWin.Value = 100); // UIスレッドで更新
|
206
|
+
|
207
|
+
Thread.Sleep(500); // 100%見えてから
|
208
|
+
});
|
209
|
+
xWin.Close();
|
210
|
+
|
211
|
+
MainWindow.Show();
|
212
|
+
}
|
213
|
+
}
|
214
|
+
```
|