Dreamine.UI.Wpf.Controls
1.0.1
Dreamine.UI.Wpf.Controls 사용자 인터페이스 기능과 구성 요소를 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
DreamineMessageBoxWindow.xaml.cs
이 파일의 문서화 페이지로 가기
1
using
System.Threading;
2
using
System.Windows;
3
using
System.Windows.Controls;
4
5
namespace
Dreamine.UI.Wpf.Controls.MessageBox
6
{
15
public
partial class
DreamineMessageBoxWindow
: Window
16
{
25
public
MessageBoxResult
Result
{
get
;
private
set
; } = MessageBoxResult.None;
26
35
private
readonly CancellationTokenSource
_cts
=
new
();
36
101
public
DreamineMessageBoxWindow
(
string
title,
string
message, MessageBoxImage icon, MessageBoxButton buttons, MessageBoxResult autoClickTarget = MessageBoxResult.None,
int
autoClickDelaySeconds = 0,
int
enableDelaySeconds = 0)
102
{
103
InitializeComponent();
104
105
TxtTitle.Text = title;
106
TxtMessage.Text = message;
107
TxtIcon.Text =
GetEmoji
(icon);
108
109
// 버튼 구성
110
SetupButtons
(buttons);
111
112
if
(enableDelaySeconds > 0)
113
_ =
DisableButtonsAndEnableLaterAsync
(enableDelaySeconds,
_cts
.Token);
114
115
if
(autoClickTarget != MessageBoxResult.None && autoClickDelaySeconds > 0)
116
_ =
StartAutoClickAsync
(autoClickTarget, autoClickDelaySeconds,
_cts
.Token);
117
}
118
135
protected
override
void
OnClosed
(EventArgs e)
136
{
137
_cts
.Cancel();
138
_cts
.Dispose();
139
base.OnClosed(e);
140
}
141
174
private
async Task
DisableButtonsAndEnableLaterAsync
(
int
delaySeconds, CancellationToken ct =
default
)
175
{
176
var buttons =
new
[] { BtnOk, BtnYes, BtnNo, BtnCancel };
177
178
// 버튼별 원래 텍스트 저장
179
var originalTexts = buttons.ToDictionary(btn => btn, btn => btn.Content?.ToString());
180
181
foreach
(var btn
in
buttons)
182
btn.IsEnabled =
false
;
183
184
for
(
int
i = delaySeconds; i > 0; i--)
185
{
186
if
(ct.IsCancellationRequested)
return
;
187
188
foreach
(var btn
in
buttons.Where(b => b.Visibility == Visibility.Visible))
189
btn.Content = $
"{originalTexts[btn]} ({i})"
;
190
191
try
{ await Task.Delay(1000, ct); }
192
catch
(OperationCanceledException) {
return
; }
193
}
194
195
foreach
(var btn
in
buttons)
196
{
197
if
(btn.Visibility == Visibility.Visible)
198
{
199
btn.IsEnabled =
true
;
200
btn.Content = originalTexts[btn];
201
}
202
}
203
}
204
245
private
async Task
StartAutoClickAsync
(MessageBoxResult target,
int
delaySeconds, CancellationToken ct =
default
)
246
{
247
var button =
GetTargetButton
(target);
248
if
(button ==
null
)
return
;
249
250
string
originalText = button.Content?.ToString() ??
""
;
251
252
for
(
int
i = delaySeconds; i > 0; i--)
253
{
254
if
(ct.IsCancellationRequested)
return
;
255
256
button.Content = $
"{originalText} ({i})"
;
257
258
try
{ await Task.Delay(1000, ct); }
259
catch
(OperationCanceledException) {
return
; }
260
}
261
262
if
(ct.IsCancellationRequested)
return
;
263
button.Content = originalText;
264
265
switch
(target)
266
{
267
case
MessageBoxResult.OK:
BtnOk_Click
(
null
!,
null
!);
break
;
268
case
MessageBoxResult.Yes:
BtnYes_Click
(
null
!,
null
!);
break
;
269
case
MessageBoxResult.No:
BtnNo_Click
(
null
!,
null
!);
break
;
270
case
MessageBoxResult.Cancel:
BtnCancel_Click
(
null
!,
null
!);
break
;
271
}
272
}
273
298
private
Button?
GetTargetButton
(MessageBoxResult result) => result
switch
299
{
300
MessageBoxResult.OK => BtnOk,
301
MessageBoxResult.Cancel => BtnCancel,
302
MessageBoxResult.Yes => BtnYes,
303
MessageBoxResult.No => BtnNo,
304
_ =>
null
305
};
306
331
private
string
GetEmoji
(MessageBoxImage icon)
332
{
333
return
icon
switch
334
{
335
MessageBoxImage.Error =>
"❌"
,
336
MessageBoxImage.Warning =>
"⚠️"
,
337
MessageBoxImage.Information =>
"ℹ️"
,
338
MessageBoxImage.Question =>
"❓"
,
339
_ =>
"🔔"
340
};
341
}
342
359
private
void
SetupButtons
(MessageBoxButton buttons)
360
{
361
BtnOk.Visibility = Visibility.Collapsed;
362
BtnYes.Visibility = Visibility.Collapsed;
363
BtnNo.Visibility = Visibility.Collapsed;
364
BtnCancel.Visibility = Visibility.Collapsed;
365
366
switch
(buttons)
367
{
368
case
MessageBoxButton.OK:
369
BtnOk.Focus();
370
BtnOk.Visibility = Visibility.Visible;
371
break
;
372
case
MessageBoxButton.OKCancel:
373
BtnOk.Focus();
374
BtnOk.Visibility = Visibility.Visible;
375
BtnCancel.Visibility = Visibility.Visible;
376
break
;
377
case
MessageBoxButton.YesNo:
378
BtnYes.Focus();
379
BtnYes.Visibility = Visibility.Visible;
380
BtnNo.Visibility = Visibility.Visible;
381
break
;
382
case
MessageBoxButton.YesNoCancel:
383
BtnYes.Focus();
384
BtnYes.Visibility = Visibility.Visible;
385
BtnNo.Visibility = Visibility.Visible;
386
BtnCancel.Visibility = Visibility.Visible;
387
break
;
388
}
389
}
390
415
private
void
BtnOk_Click
(
object
sender, RoutedEventArgs e)
416
{
417
Result
= MessageBoxResult.OK;
418
Close();
419
}
420
445
private
void
BtnCancel_Click
(
object
sender, RoutedEventArgs e)
446
{
447
Result
= MessageBoxResult.Cancel;
448
Close();
449
}
450
475
private
void
BtnYes_Click
(
object
sender, RoutedEventArgs e)
476
{
477
Result
= MessageBoxResult.Yes;
478
Close();
479
}
480
505
private
void
BtnNo_Click
(
object
sender, RoutedEventArgs e)
506
{
507
Result
= MessageBoxResult.No;
508
Close();
509
}
510
}
511
}
Dreamine.UI.Wpf.Controls.MessageBox
Definition
DreamineMessageBox.cs:4
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.OnClosed
override void OnClosed(EventArgs e)
Definition
DreamineMessageBoxWindow.xaml.cs:135
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.BtnOk_Click
void BtnOk_Click(object sender, RoutedEventArgs e)
Definition
DreamineMessageBoxWindow.xaml.cs:415
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow._cts
readonly CancellationTokenSource _cts
Definition
DreamineMessageBoxWindow.xaml.cs:35
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.GetEmoji
string GetEmoji(MessageBoxImage icon)
Definition
DreamineMessageBoxWindow.xaml.cs:331
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.StartAutoClickAsync
async Task StartAutoClickAsync(MessageBoxResult target, int delaySeconds, CancellationToken ct=default)
Definition
DreamineMessageBoxWindow.xaml.cs:245
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.BtnNo_Click
void BtnNo_Click(object sender, RoutedEventArgs e)
Definition
DreamineMessageBoxWindow.xaml.cs:505
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.SetupButtons
void SetupButtons(MessageBoxButton buttons)
Definition
DreamineMessageBoxWindow.xaml.cs:359
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.BtnCancel_Click
void BtnCancel_Click(object sender, RoutedEventArgs e)
Definition
DreamineMessageBoxWindow.xaml.cs:445
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.GetTargetButton
Button? GetTargetButton(MessageBoxResult result)
Definition
DreamineMessageBoxWindow.xaml.cs:298
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.BtnYes_Click
void BtnYes_Click(object sender, RoutedEventArgs e)
Definition
DreamineMessageBoxWindow.xaml.cs:475
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.DisableButtonsAndEnableLaterAsync
async Task DisableButtonsAndEnableLaterAsync(int delaySeconds, CancellationToken ct=default)
Definition
DreamineMessageBoxWindow.xaml.cs:174
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.Result
MessageBoxResult Result
Definition
DreamineMessageBoxWindow.xaml.cs:25
Dreamine.UI.Wpf.Controls.MessageBox.DreamineMessageBoxWindow.DreamineMessageBoxWindow
DreamineMessageBoxWindow(string title, string message, MessageBoxImage icon, MessageBoxButton buttons, MessageBoxResult autoClickTarget=MessageBoxResult.None, int autoClickDelaySeconds=0, int enableDelaySeconds=0)
Definition
DreamineMessageBoxWindow.xaml.cs:101
MessageBox
DreamineMessageBoxWindow.xaml.cs
다음에 의해 생성됨 :
1.17.0