回答編集履歴

2

動画追加

2024/08/31 16:35

投稿

TN8001
TN8001

スコア9855

test CHANGED
@@ -1,4 +1,4 @@
1
- まずMessageBoxWindowはクライアント領域内を完全に覆うのでしょうから単に、
1
+ まず`MessageBoxWindow`はクライアント領域内を完全に覆うのでしょうから単に、
2
2
  ```xml
3
3
  <Window>
4
4
  <Grid>
@@ -16,8 +16,24 @@
16
16
 
17
17
  ---
18
18
 
19
+ ```xml:MainWindow.xaml
19
- MainWindow
20
+ <Window
21
+ x:Class="Questions227100.MainWindow"
22
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
23
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
+ xmlns:window="clr-namespace:Questions227100"
25
+ Width="800"
26
+ Height="450">
27
+ <Grid>
28
+ <window:MessageBoxWindow x:Name="messageBoxWindow" />
29
+
30
+ <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
31
+ <!-- ↑の代わりにただのボタン -->
32
+ <Button Click="Button_Click" Content="何かを実行" />
33
+ </Grid>
34
+ </Window>
20
- ```cs
35
+ ```
36
+ ```cs:MainWindow.xaml.cs
21
37
  using System.Diagnostics;
22
38
  using System.Windows;
23
39
 
@@ -38,28 +54,56 @@
38
54
  }
39
55
  ```
40
56
 
57
+ ---
58
+
41
- ```xml
59
+ ```xml:MessageBoxWindow.xaml
42
- <Window
60
+ <UserControl
43
- x:Class="Questions227100.MainWindow"
61
+ x:Class="Questions227100.MessageBoxWindow"
44
62
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
63
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
64
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
65
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
+ d:DesignHeight="450"
67
+ d:DesignWidth="800"
46
- xmlns:window="clr-namespace:Questions227100"
68
+ Background="#4C000000"
69
+ mc:Ignorable="d">
70
+ <Border
47
- Width="800"
71
+ MaxWidth="500"
48
- Height="450">
72
+ MaxHeight="300"
73
+ VerticalAlignment="Center"
74
+ Background="#FFEEEEEE"
75
+ BorderBrush="#FF0071EF"
76
+ BorderThickness="0,5,0,0">
49
- <Grid>
77
+ <Grid>
78
+ <Grid.RowDefinitions>
79
+ <RowDefinition />
50
- <window:MessageBoxWindow x:Name="messageBoxWindow" />
80
+ <RowDefinition Height="Auto" />
51
-
81
+ </Grid.RowDefinitions>
82
+ <TextBlock
83
+ x:Name="MessageText"
84
+ Margin="10"
85
+ FontWeight="Bold"
86
+ TextWrapping="Wrap" />
87
+ <StackPanel
88
+ Grid.Row="1"
89
+ HorizontalAlignment="Right"
90
+ Orientation="Horizontal">
91
+ <Button
92
+ MinWidth="90"
93
+ Margin="10"
94
+ Click="ButtonOK_Click"
95
+ Content="OK" />
96
+ <Button
97
+ MinWidth="90"
98
+ Margin="10"
99
+ Click="ButtonCancel_Click"
52
- <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
100
+ Content="Cancel" />
53
- <!-- ↑の代わりにただのボタン -->
101
+ </StackPanel>
54
- <Button Click="Button_Click" Content="何かを実行" />
55
- </Grid>
102
+ </Grid>
56
- </Window>
103
+ </Border>
104
+ </UserControl>
57
105
  ```
58
-
59
- ---
60
-
61
- MessageBoxWindow
106
+ ```cs:MessageBoxWindow.xaml.cs
62
- ```cs
63
107
  using System;
64
108
  using System.Collections.Generic;
65
109
  using System.Linq;
@@ -140,54 +184,7 @@
140
184
  }
141
185
  }
142
186
  ```
143
-
144
- ```xml
145
- <UserControl
146
- x:Class="Questions227100.MessageBoxWindow"
147
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
187
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-09-01/e9e72aa7-c2a4-4874-bf48-7bcb6cd49ebf.gif)
148
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
149
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
150
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
151
- d:DesignHeight="450"
152
- d:DesignWidth="800"
153
- Background="#4C000000"
154
- mc:Ignorable="d">
155
- <Border
156
- MaxWidth="500"
157
- MaxHeight="300"
158
- VerticalAlignment="Center"
159
- Background="#FFEEEEEE"
160
- BorderBrush="#FF0071EF"
161
- BorderThickness="0,5,0,0">
162
- <Grid>
163
- <Grid.RowDefinitions>
164
- <RowDefinition />
165
- <RowDefinition Height="Auto" />
166
- </Grid.RowDefinitions>
167
- <TextBlock
168
- x:Name="MessageText"
169
- Margin="10"
170
- FontWeight="Bold"
171
- TextWrapping="Wrap" />
172
- <StackPanel
173
- Grid.Row="1"
174
- HorizontalAlignment="Right"
175
- Orientation="Horizontal">
176
- <Button
177
- MinWidth="90"
178
- Margin="10"
179
- Click="ButtonOK_Click"
180
- Content="OK" />
181
- <Button
182
- MinWidth="90"
183
- Margin="10"
184
- Click="ButtonCancel_Click"
185
- Content="Cancel" />
186
- </StackPanel>
187
- </Grid>
188
- </Border>
189
- </UserControl>
190
- ```
191
188
 
192
189
  ---
193
190
 

1

リンク切れ

2022/09/15 09:53

投稿

TN8001
TN8001

スコア9855

test CHANGED
@@ -1,397 +1,199 @@
1
1
  まずMessageBoxWindowはクライアント領域内を完全に覆うのでしょうから単に、
2
-
3
- ```xaml
2
+ ```xml
4
-
5
3
  <Window>
6
-
7
4
  <Grid>
8
-
9
5
  <window:MessageBoxWindow />
10
-
11
6
  <window:LanguageMenuWindow />
12
-
13
7
  </Grid>
14
-
15
8
  </Window>
16
-
17
9
  ```
18
-
19
10
  こういう形でいいんじゃないでしょうか。
20
11
 
21
-
22
-
23
12
  バインディングをうまく使えば、割とすっきりできそうにも思うのですが、
24
-
25
13
  > MessageBoxResult flag = MessageBox.Show("OKですか?");
26
-
27
-
28
14
 
29
15
  こういうイメージとのことでちょっと無理やり感もありますが、今のコードをあまり壊さない形でやってみました。
30
16
 
17
+ ---
31
18
 
19
+ MainWindow
20
+ ```cs
21
+ using System.Diagnostics;
22
+ using System.Windows;
23
+
24
+ namespace Questions227100
25
+ {
26
+ public partial class MainWindow : Window
27
+ {
28
+ public MainWindow() => InitializeComponent();
29
+
30
+ private async void Button_Click(object sender, RoutedEventArgs e)
31
+ {
32
+ var result = await MessageBoxWindow.ShowAsync(this, "いいんですね?");
33
+
34
+ if(result == MessageBoxResult.OK) Debug.WriteLine("OK");
35
+ if(result == MessageBoxResult.Cancel) Debug.WriteLine("Cancel");
36
+ }
37
+ }
38
+ }
39
+ ```
40
+
41
+ ```xml
42
+ <Window
43
+ x:Class="Questions227100.MainWindow"
44
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
46
+ xmlns:window="clr-namespace:Questions227100"
47
+ Width="800"
48
+ Height="450">
49
+ <Grid>
50
+ <window:MessageBoxWindow x:Name="messageBoxWindow" />
51
+
52
+ <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
53
+ <!-- ↑の代わりにただのボタン -->
54
+ <Button Click="Button_Click" Content="何かを実行" />
55
+ </Grid>
56
+ </Window>
57
+ ```
32
58
 
33
59
  ---
34
60
 
35
-
36
-
37
- MainWindow
61
+ MessageBoxWindow
38
-
39
- ```C#
62
+ ```cs
40
-
63
+ using System;
64
+ using System.Collections.Generic;
41
- using System.Diagnostics;
65
+ using System.Linq;
42
-
66
+ using System.Threading.Tasks;
43
67
  using System.Windows;
44
-
68
+ using System.Windows.Controls;
45
-
69
+ using System.Windows.Media;
46
70
 
47
71
  namespace Questions227100
72
+ {
73
+ public partial class MessageBoxWindow : UserControl
74
+ {
75
+ private TaskCompletionSource<MessageBoxResult> taskCompletionSource;
48
76
 
49
- {
50
-
51
- public partial class MainWindow : Window
77
+ public MessageBoxWindow()
52
-
53
- {
54
-
55
- public MainWindow() => InitializeComponent();
56
-
57
-
58
-
59
- private async void Button_Click(object sender, RoutedEventArgs e)
60
-
61
78
  {
62
-
63
- var result = await MessageBoxWindow.ShowAsync(this, "いいんですね?");
79
+ InitializeComponent();
64
-
65
-
66
-
67
- if(result == MessageBoxResult.OK) Debug.WriteLine("OK");
80
+ SetValue(Panel.ZIndexProperty, 10);
68
-
69
- if(result == MessageBoxResult.Cancel) Debug.WriteLine("Cancel");
81
+ Visibility = Visibility.Hidden;
70
-
71
82
  }
72
83
 
73
- }
74
-
75
- }
76
-
77
- ```
78
-
79
-
80
-
81
- ```xaml
82
-
83
- <Window
84
-
85
- x:Class="Questions227100.MainWindow"
86
-
87
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
88
-
89
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
90
-
91
- xmlns:window="clr-namespace:Questions227100"
92
-
93
- Width="800"
94
-
95
- Height="450">
96
-
97
- <Grid>
98
-
99
- <window:MessageBoxWindow x:Name="messageBoxWindow" />
100
-
101
-
102
-
103
- <!--<window:LanguageMenuWindow x:Name="LanguageMenuWindow" />-->
104
-
105
- <!-- ↑の代わりにただのボタン -->
106
-
107
- <Button Click="Button_Click" Content="何かを実行" />
108
-
109
- </Grid>
110
-
111
- </Window>
112
-
113
- ```
114
-
115
-
116
-
117
- ---
118
-
119
-
120
-
121
- MessageBoxWindow
122
-
123
- ```C#
124
-
125
- using System;
126
-
127
- using System.Collections.Generic;
128
-
129
- using System.Linq;
130
-
131
- using System.Threading.Tasks;
132
-
133
- using System.Windows;
134
-
135
- using System.Windows.Controls;
136
-
137
- using System.Windows.Media;
138
-
139
-
140
-
141
- namespace Questions227100
142
-
143
- {
144
-
145
- public partial class MessageBoxWindow : UserControl
146
-
147
- {
148
-
149
- private TaskCompletionSource<MessageBoxResult> taskCompletionSource;
84
+ public static Task<MessageBoxResult> ShowAsync(UIElement element, string message)
150
-
151
-
152
-
153
- public MessageBoxWindow()
154
-
155
85
  {
156
-
157
- InitializeComponent();
86
+ var win = Window.GetWindow(element);
158
-
87
+ var msgbox = win.Descendants<MessageBoxWindow>().FirstOrDefault();
88
+ if(msgbox == null) throw new InvalidOperationException("MessageBoxWindowが見つかりません。");
159
- SetValue(Panel.ZIndexProperty, 10);
89
+ return msgbox.ShowAsync(message);
160
-
90
+ }
91
+ public Task<MessageBoxResult> ShowAsync(string message)
92
+ {
93
+ MessageText.Text = message;
161
- Visibility = Visibility.Hidden;
94
+ Visibility = Visibility.Visible;
162
-
95
+ taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
96
+ return taskCompletionSource.Task;
163
97
  }
164
98
 
165
-
166
-
167
- public static Task<MessageBoxResult> ShowAsync(UIElement element, string message)
99
+ private void ButtonOK_Click(object sender, RoutedEventArgs e)
168
-
169
100
  {
170
-
171
- var win = Window.GetWindow(element);
101
+ Visibility = Visibility.Hidden;
172
-
173
- var msgbox = win.Descendants<MessageBoxWindow>().FirstOrDefault();
102
+ taskCompletionSource.SetResult(MessageBoxResult.OK);
174
-
175
- if(msgbox == null) throw new InvalidOperationException("MessageBoxWindowが見つかりません。");
176
-
177
- return msgbox.ShowAsync(message);
178
-
179
103
  }
180
-
181
- public Task<MessageBoxResult> ShowAsync(string message)
104
+ private void ButtonCancel_Click(object sender, RoutedEventArgs e)
182
-
183
105
  {
184
-
185
- MessageText.Text = message;
186
-
187
- Visibility = Visibility.Visible;
106
+ Visibility = Visibility.Hidden;
188
-
189
- taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
107
+ taskCompletionSource.SetResult(MessageBoxResult.Cancel);
190
-
191
- return taskCompletionSource.Task;
192
-
193
108
  }
194
-
195
-
196
-
197
- private void ButtonOK_Click(object sender, RoutedEventArgs e)
198
-
199
- {
200
-
201
- Visibility = Visibility.Hidden;
202
-
203
- taskCompletionSource.SetResult(MessageBoxResult.OK);
204
-
205
- }
206
-
207
- private void ButtonCancel_Click(object sender, RoutedEventArgs e)
208
-
209
- {
210
-
211
- Visibility = Visibility.Hidden;
212
-
213
- taskCompletionSource.SetResult(MessageBoxResult.Cancel);
214
-
215
- }
216
-
217
109
  }
218
110
 
219
111
 
220
112
 
221
-
222
-
223
-
224
-
225
113
  //https://blog.xin9le.net/entry/2013/10/29/222336
226
-
227
114
  internal static class DependencyObjectExtensions
228
-
229
115
  {
230
-
231
116
  public static IEnumerable<DependencyObject> Children(this DependencyObject obj)
232
-
233
117
  {
234
-
235
118
  if(obj == null) throw new ArgumentNullException(nameof(obj));
236
-
237
119
  var count = VisualTreeHelper.GetChildrenCount(obj);
238
-
239
120
  if(count == 0) yield break;
240
-
241
121
  for(var i = 0; i < count; i++)
242
-
243
122
  {
244
-
245
123
  var child = VisualTreeHelper.GetChild(obj, i);
246
-
247
124
  if(child != null) yield return child;
248
-
249
125
  }
250
-
251
126
  }
252
-
253
127
  public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj)
254
-
255
128
  {
256
-
257
129
  if(obj == null) throw new ArgumentNullException(nameof(obj));
258
-
259
130
  foreach(var child in obj.Children())
260
-
261
131
  {
262
-
263
132
  yield return child;
264
-
265
133
  foreach(var grandChild in child.Descendants()) yield return grandChild;
266
-
267
134
  }
268
-
269
135
  }
270
-
271
136
  public static IEnumerable<T> Children<T>(this DependencyObject obj)
272
-
273
137
  where T : DependencyObject => obj.Children().OfType<T>();
274
-
275
138
  public static IEnumerable<T> Descendants<T>(this DependencyObject obj)
276
-
277
139
  where T : DependencyObject => obj.Descendants().OfType<T>();
278
-
279
140
  }
280
-
281
141
  }
282
-
283
142
  ```
284
143
 
285
-
286
-
287
- ```xaml
144
+ ```xml
288
-
289
145
  <UserControl
290
-
291
146
  x:Class="Questions227100.MessageBoxWindow"
292
-
293
147
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
294
-
295
148
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
296
-
297
149
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
298
-
299
150
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
300
-
301
151
  d:DesignHeight="450"
302
-
303
152
  d:DesignWidth="800"
304
-
305
153
  Background="#4C000000"
306
-
307
154
  mc:Ignorable="d">
308
-
309
155
  <Border
310
-
311
156
  MaxWidth="500"
312
-
313
157
  MaxHeight="300"
314
-
315
158
  VerticalAlignment="Center"
316
-
317
159
  Background="#FFEEEEEE"
318
-
319
160
  BorderBrush="#FF0071EF"
320
-
321
161
  BorderThickness="0,5,0,0">
322
-
323
162
  <Grid>
324
-
325
163
  <Grid.RowDefinitions>
326
-
327
164
  <RowDefinition />
328
-
329
165
  <RowDefinition Height="Auto" />
330
-
331
166
  </Grid.RowDefinitions>
332
-
333
167
  <TextBlock
334
-
335
168
  x:Name="MessageText"
336
-
337
169
  Margin="10"
338
-
339
170
  FontWeight="Bold"
340
-
341
171
  TextWrapping="Wrap" />
342
-
343
172
  <StackPanel
344
-
345
173
  Grid.Row="1"
346
-
347
174
  HorizontalAlignment="Right"
348
-
349
175
  Orientation="Horizontal">
350
-
351
176
  <Button
352
-
353
177
  MinWidth="90"
354
-
355
178
  Margin="10"
356
-
357
179
  Click="ButtonOK_Click"
358
-
359
180
  Content="OK" />
360
-
361
181
  <Button
362
-
363
182
  MinWidth="90"
364
-
365
183
  Margin="10"
366
-
367
184
  Click="ButtonCancel_Click"
368
-
369
185
  Content="Cancel" />
370
-
371
186
  </StackPanel>
372
-
373
187
  </Grid>
374
-
375
188
  </Border>
376
-
377
189
  </UserControl>
378
-
379
190
  ```
380
-
381
-
382
191
 
383
192
  ---
384
193
 
385
-
386
-
387
194
  ダイアログだけかっこよくなってもバランスがありますので、
388
-
389
- [mahapps.metro - Dialogs](https://mahapps.com/controls/dialogs.html)
195
+ [MahApps.Metro - Dialogs](https://mahapps.com/docs/dialogs/)
390
-
391
196
  [Material Design In XAML Toolkit - Dialogs](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs)
392
197
 
393
-
394
-
395
198
  こういったものに乗ってしまえば、すでに同様のものが用意されています。
396
-
397
199
  ただ導入するにもまた調整がいるので、是非にとまでは言えません。