回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,335 +1,168 @@
|
|
1
1
|
毎回newするパターン(`Window1`)と、
|
2
|
-
|
3
2
|
Show Hideするだけのパターン(`Window2`)の両方用意しました。
|
4
|
-
|
5
|
-
|
6
3
|
|
7
4
|
`MainWindow`の取得方法もいろいろ考えられますが、手間が少なそうな方法にしました。
|
8
5
|
|
9
|
-
|
10
|
-
|
11
6
|
×ボタンや終了時の処理等、地味に罠があります。
|
12
7
|
|
13
|
-
|
14
|
-
|
15
8
|
MainWindow
|
16
|
-
|
17
|
-
```x
|
9
|
+
```xml
|
18
|
-
|
19
10
|
<Window
|
20
|
-
|
21
11
|
x:Class="Questions247571.MainWindow"
|
22
|
-
|
23
12
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
24
|
-
|
25
13
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
26
|
-
|
27
14
|
Title="MainWindow"
|
28
|
-
|
29
15
|
Width="800"
|
30
|
-
|
31
16
|
Height="450">
|
32
|
-
|
33
17
|
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
|
34
|
-
|
35
18
|
<Button
|
36
|
-
|
37
19
|
Margin="10"
|
38
|
-
|
39
20
|
Click="Window1Button_Click"
|
40
|
-
|
41
21
|
Content="Window1へ" />
|
42
|
-
|
43
22
|
<Button
|
44
|
-
|
45
23
|
Margin="10"
|
46
|
-
|
47
24
|
Click="Window2Button_Click"
|
48
|
-
|
49
25
|
Content="Window2へ" />
|
50
|
-
|
51
26
|
</StackPanel>
|
52
|
-
|
53
27
|
</Window>
|
54
|
-
|
55
28
|
```
|
56
29
|
|
57
|
-
|
58
|
-
|
59
|
-
```
|
30
|
+
```cs
|
60
|
-
|
61
31
|
using System.Windows;
|
62
32
|
|
33
|
+
namespace Questions247571
|
34
|
+
{
|
35
|
+
public partial class MainWindow : Window
|
36
|
+
{
|
37
|
+
// こうだと Application.Current.MainWindow がWindow2になってしまうためNG
|
38
|
+
//private Window2 window2 = new Window2();
|
39
|
+
private Window2 window2;
|
63
40
|
|
41
|
+
public MainWindow()
|
42
|
+
{
|
43
|
+
InitializeComponent();
|
44
|
+
|
45
|
+
// Window2は閉じないのでデフォルト(OnLastWindowClose)だとアプリが終わらなくなる
|
46
|
+
// MainWindowのCloseでアプリも終了
|
47
|
+
// App.xamlで指定してよい
|
48
|
+
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
|
49
|
+
|
50
|
+
window2 = new Window2();
|
51
|
+
}
|
52
|
+
|
53
|
+
private void Window1Button_Click(object sender, RoutedEventArgs e)
|
54
|
+
{
|
55
|
+
var window1 = new Window1();
|
56
|
+
window1.Show();
|
57
|
+
this.Hide();
|
58
|
+
}
|
59
|
+
private void Window2Button_Click(object sender, RoutedEventArgs e)
|
60
|
+
{
|
61
|
+
window2.Show();
|
62
|
+
this.Hide();
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
Window1
|
69
|
+
```xml
|
70
|
+
<Window
|
71
|
+
x:Class="Questions247571.Window1"
|
72
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
73
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
74
|
+
Title="Window1"
|
75
|
+
Width="800"
|
76
|
+
Height="450">
|
77
|
+
<Grid>
|
78
|
+
<TextBox Text="文字を書き換えてみよう
毎回新しく作るので状態は保存されない" />
|
79
|
+
<Button
|
80
|
+
HorizontalAlignment="Center"
|
81
|
+
VerticalAlignment="Center"
|
82
|
+
Click="Button_Click"
|
83
|
+
Content="MainWindowへ" />
|
84
|
+
</Grid>
|
85
|
+
</Window>
|
86
|
+
```
|
87
|
+
|
88
|
+
```cs
|
89
|
+
using System.ComponentModel;
|
90
|
+
using System.Windows;
|
64
91
|
|
65
92
|
namespace Questions247571
|
93
|
+
{
|
94
|
+
public partial class Window1 : Window
|
95
|
+
{
|
96
|
+
public Window1() => InitializeComponent();
|
66
97
|
|
98
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
99
|
+
{
|
100
|
+
// OnClosingへ
|
101
|
+
// ×ボタンで閉じるとき等とまとめて処理
|
102
|
+
this.Close();
|
103
|
+
}
|
104
|
+
protected override void OnClosing(CancelEventArgs e)
|
105
|
+
{
|
106
|
+
base.OnClosing(e);
|
107
|
+
|
108
|
+
Application.Current.MainWindow.Show();
|
109
|
+
// このあと閉じる
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
Window2
|
116
|
+
```xml
|
117
|
+
<Window
|
118
|
+
x:Class="Questions247571.Window2"
|
119
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
120
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
121
|
+
Title="Window2"
|
122
|
+
Width="800"
|
123
|
+
Height="450">
|
124
|
+
<Grid>
|
125
|
+
<TextBox Text="文字を書き換えてみよう
非表示になってるだけなので状態は保存される" />
|
126
|
+
<Button
|
127
|
+
HorizontalAlignment="Center"
|
128
|
+
VerticalAlignment="Center"
|
129
|
+
Click="Button_Click"
|
130
|
+
Content="MainWindowへ" />
|
131
|
+
</Grid>
|
132
|
+
</Window>
|
133
|
+
```
|
134
|
+
|
135
|
+
```cs
|
136
|
+
using System.ComponentModel;
|
137
|
+
using System.Windows;
|
138
|
+
|
139
|
+
namespace Questions247571
|
67
140
|
{
|
141
|
+
public partial class Window2 : Window
|
142
|
+
{
|
143
|
+
public Window2() => InitializeComponent();
|
68
144
|
|
145
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
146
|
+
{
|
147
|
+
// OnClosingへ
|
148
|
+
// ×ボタンで閉じるとき等とまとめて処理
|
149
|
+
this.Close();
|
150
|
+
}
|
69
|
-
p
|
151
|
+
protected override void OnClosing(CancelEventArgs e)
|
152
|
+
{
|
153
|
+
base.OnClosing(e);
|
70
154
|
|
71
|
-
|
155
|
+
// アプリ終了時に NullReferenceException が出るので ?. にしておく
|
72
|
-
|
73
|
-
|
156
|
+
Application.Current.MainWindow?.Show();
|
74
|
-
|
75
|
-
//private Window2 window2 = new Window2();
|
76
|
-
|
77
|
-
private Window2 window2;
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
public MainWindow()
|
82
|
-
|
83
|
-
{
|
84
|
-
|
85
|
-
InitializeComponent();
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
// Window2は閉じないのでデフォルト(OnLastWindowClose)だとアプリが終わらなくなる
|
90
|
-
|
91
|
-
// MainWindowのCloseでアプリも終了
|
92
|
-
|
93
|
-
// App.xamlで指定してよい
|
94
|
-
|
95
|
-
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
window2 = new Window2();
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
private void Window1Button_Click(object sender, RoutedEventArgs e)
|
106
|
-
|
107
|
-
{
|
108
|
-
|
109
|
-
var window1 = new Window1();
|
110
|
-
|
111
|
-
window1.Show();
|
112
|
-
|
113
157
|
this.Hide();
|
114
158
|
|
159
|
+
// 閉じないようにキャンセル
|
160
|
+
e.Cancel = true;
|
115
161
|
}
|
116
|
-
|
117
|
-
private void Window2Button_Click(object sender, RoutedEventArgs e)
|
118
|
-
|
119
|
-
{
|
120
|
-
|
121
|
-
window2.Show();
|
122
|
-
|
123
|
-
this.Hide();
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
162
|
}
|
128
|
-
|
129
163
|
}
|
130
|
-
|
131
164
|
```
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
Window1
|
136
|
-
|
137
|
-
```xaml
|
138
|
-
|
139
|
-
<Window
|
140
|
-
|
141
|
-
x:Class="Questions247571.Window1"
|
142
|
-
|
143
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
144
|
-
|
145
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
146
|
-
|
147
|
-
Title="Window1"
|
148
|
-
|
149
|
-
Width="800"
|
150
|
-
|
151
|
-
Height="450">
|
152
|
-
|
153
|
-
<Grid>
|
154
|
-
|
155
|
-
<TextBox Text="文字を書き換えてみよう
毎回新しく作るので状態は保存されない" />
|
156
|
-
|
157
|
-
<Button
|
158
|
-
|
159
|
-
HorizontalAlignment="Center"
|
160
|
-
|
161
|
-
VerticalAlignment="Center"
|
162
|
-
|
163
|
-
Click="Button_Click"
|
164
|
-
|
165
|
-
Content="MainWindowへ" />
|
166
|
-
|
167
|
-
</Grid>
|
168
|
-
|
169
|
-
</Window>
|
170
|
-
|
171
|
-
```
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
```C#
|
176
|
-
|
177
|
-
using System.ComponentModel;
|
178
|
-
|
179
|
-
using System.Windows;
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
namespace Questions247571
|
184
|
-
|
185
|
-
{
|
186
|
-
|
187
|
-
public partial class Window1 : Window
|
188
|
-
|
189
|
-
{
|
190
|
-
|
191
|
-
public Window1() => InitializeComponent();
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
196
|
-
|
197
|
-
{
|
198
|
-
|
199
|
-
// OnClosingへ
|
200
|
-
|
201
|
-
// ×ボタンで閉じるとき等とまとめて処理
|
202
|
-
|
203
|
-
this.Close();
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
protected override void OnClosing(CancelEventArgs e)
|
208
|
-
|
209
|
-
{
|
210
|
-
|
211
|
-
base.OnClosing(e);
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
Application.Current.MainWindow.Show();
|
216
|
-
|
217
|
-
// このあと閉じる
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
```
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
Window2
|
230
|
-
|
231
|
-
```xaml
|
232
|
-
|
233
|
-
<Window
|
234
|
-
|
235
|
-
x:Class="Questions247571.Window2"
|
236
|
-
|
237
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
238
|
-
|
239
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
240
|
-
|
241
|
-
Title="Window2"
|
242
|
-
|
243
|
-
Width="800"
|
244
|
-
|
245
|
-
Height="450">
|
246
|
-
|
247
|
-
<Grid>
|
248
|
-
|
249
|
-
<TextBox Text="文字を書き換えてみよう
非表示になってるだけなので状態は保存される" />
|
250
|
-
|
251
|
-
<Button
|
252
|
-
|
253
|
-
HorizontalAlignment="Center"
|
254
|
-
|
255
|
-
VerticalAlignment="Center"
|
256
|
-
|
257
|
-
Click="Button_Click"
|
258
|
-
|
259
|
-
Content="MainWindowへ" />
|
260
|
-
|
261
|
-
</Grid>
|
262
|
-
|
263
|
-
</Window>
|
264
|
-
|
265
|
-
```
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
```C#
|
270
|
-
|
271
|
-
using System.ComponentModel;
|
272
|
-
|
273
|
-
using System.Windows;
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
namespace Questions247571
|
278
|
-
|
279
|
-
{
|
280
|
-
|
281
|
-
public partial class Window2 : Window
|
282
|
-
|
283
|
-
{
|
284
|
-
|
285
|
-
public Window2() => InitializeComponent();
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
290
|
-
|
291
|
-
{
|
292
|
-
|
293
|
-
// OnClosingへ
|
294
|
-
|
295
|
-
// ×ボタンで閉じるとき等とまとめて処理
|
296
|
-
|
297
|
-
this.Close();
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
protected override void OnClosing(CancelEventArgs e)
|
302
|
-
|
303
|
-
{
|
304
|
-
|
305
|
-
base.OnClosing(e);
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
// アプリ終了時に NullReferenceException が出るので ?. にしておく
|
310
|
-
|
311
|
-
Application.Current.MainWindow?.Show();
|
312
|
-
|
313
|
-
this.Hide();
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
// 閉じないようにキャンセル
|
318
|
-
|
319
|
-
e.Cancel = true;
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
```
|
328
|
-
|
329
|
-
|
330
165
|
|
331
166
|
---
|
332
167
|
|
333
|
-
|
334
|
-
|
335
168
|
個人的には`Window`を次々出すより、`Page`として作成し`MainWindow`内で入れ替えるような作りが好みです。
|