回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,168 +1,168 @@
|
|
1
|
-
毎回newするパターン(`Window1`)と、
|
2
|
-
Show Hideするだけのパターン(`Window2`)の両方用意しました。
|
3
|
-
|
4
|
-
`MainWindow`の取得方法もいろいろ考えられますが、手間が少なそうな方法にしました。
|
5
|
-
|
6
|
-
×ボタンや終了時の処理等、地味に罠があります。
|
7
|
-
|
8
|
-
MainWindow
|
9
|
-
```
|
10
|
-
<Window
|
11
|
-
x:Class="Questions247571.MainWindow"
|
12
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
13
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
14
|
-
Title="MainWindow"
|
15
|
-
Width="800"
|
16
|
-
Height="450">
|
17
|
-
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
|
18
|
-
<Button
|
19
|
-
Margin="10"
|
20
|
-
Click="Window1Button_Click"
|
21
|
-
Content="Window1へ" />
|
22
|
-
<Button
|
23
|
-
Margin="10"
|
24
|
-
Click="Window2Button_Click"
|
25
|
-
Content="Window2へ" />
|
26
|
-
</StackPanel>
|
27
|
-
</Window>
|
28
|
-
```
|
29
|
-
|
30
|
-
```
|
31
|
-
using System.Windows;
|
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;
|
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
|
-
```
|
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
|
-
```
|
89
|
-
using System.ComponentModel;
|
90
|
-
using System.Windows;
|
91
|
-
|
92
|
-
namespace Questions247571
|
93
|
-
{
|
94
|
-
public partial class Window1 : Window
|
95
|
-
{
|
96
|
-
public Window1() => InitializeComponent();
|
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
|
-
```
|
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
|
-
```
|
136
|
-
using System.ComponentModel;
|
137
|
-
using System.Windows;
|
138
|
-
|
139
|
-
namespace Questions247571
|
140
|
-
{
|
141
|
-
public partial class Window2 : Window
|
142
|
-
{
|
143
|
-
public Window2() => InitializeComponent();
|
144
|
-
|
145
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
146
|
-
{
|
147
|
-
// OnClosingへ
|
148
|
-
// ×ボタンで閉じるとき等とまとめて処理
|
149
|
-
this.Close();
|
150
|
-
}
|
151
|
-
protected override void OnClosing(CancelEventArgs e)
|
152
|
-
{
|
153
|
-
base.OnClosing(e);
|
154
|
-
|
155
|
-
// アプリ終了時に NullReferenceException が出るので ?. にしておく
|
156
|
-
Application.Current.MainWindow?.Show();
|
157
|
-
this.Hide();
|
158
|
-
|
159
|
-
// 閉じないようにキャンセル
|
160
|
-
e.Cancel = true;
|
161
|
-
}
|
162
|
-
}
|
163
|
-
}
|
164
|
-
```
|
165
|
-
|
166
|
-
---
|
167
|
-
|
1
|
+
毎回newするパターン(`Window1`)と、
|
2
|
+
Show Hideするだけのパターン(`Window2`)の両方用意しました。
|
3
|
+
|
4
|
+
`MainWindow`の取得方法もいろいろ考えられますが、手間が少なそうな方法にしました。
|
5
|
+
|
6
|
+
×ボタンや終了時の処理等、地味に罠があります。
|
7
|
+
|
8
|
+
MainWindow
|
9
|
+
```xml
|
10
|
+
<Window
|
11
|
+
x:Class="Questions247571.MainWindow"
|
12
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
13
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
14
|
+
Title="MainWindow"
|
15
|
+
Width="800"
|
16
|
+
Height="450">
|
17
|
+
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
|
18
|
+
<Button
|
19
|
+
Margin="10"
|
20
|
+
Click="Window1Button_Click"
|
21
|
+
Content="Window1へ" />
|
22
|
+
<Button
|
23
|
+
Margin="10"
|
24
|
+
Click="Window2Button_Click"
|
25
|
+
Content="Window2へ" />
|
26
|
+
</StackPanel>
|
27
|
+
</Window>
|
28
|
+
```
|
29
|
+
|
30
|
+
```cs
|
31
|
+
using System.Windows;
|
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;
|
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;
|
91
|
+
|
92
|
+
namespace Questions247571
|
93
|
+
{
|
94
|
+
public partial class Window1 : Window
|
95
|
+
{
|
96
|
+
public Window1() => InitializeComponent();
|
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
|
140
|
+
{
|
141
|
+
public partial class Window2 : Window
|
142
|
+
{
|
143
|
+
public Window2() => InitializeComponent();
|
144
|
+
|
145
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
146
|
+
{
|
147
|
+
// OnClosingへ
|
148
|
+
// ×ボタンで閉じるとき等とまとめて処理
|
149
|
+
this.Close();
|
150
|
+
}
|
151
|
+
protected override void OnClosing(CancelEventArgs e)
|
152
|
+
{
|
153
|
+
base.OnClosing(e);
|
154
|
+
|
155
|
+
// アプリ終了時に NullReferenceException が出るので ?. にしておく
|
156
|
+
Application.Current.MainWindow?.Show();
|
157
|
+
this.Hide();
|
158
|
+
|
159
|
+
// 閉じないようにキャンセル
|
160
|
+
e.Cancel = true;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
}
|
164
|
+
```
|
165
|
+
|
166
|
+
---
|
167
|
+
|
168
168
|
個人的には`Window`を次々出すより、`Page`として作成し`MainWindow`内で入れ替えるような作りが好みです。
|