Created
April 11, 2023 10:40
-
-
Save gekka/1875f0438ca5846f9ea5a0c772d22aa5 to your computer and use it in GitHub Desktop.
UWPのContentDialogのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://social.msdn.microsoft.com/Forums/ja-JP/8a58899a-a324-48db-80c0-e0befddfee52/ | |
public async static Task<string> MessageQuestion(string title, string text, params string[] answers) | |
{ | |
StackPanel panel = new StackPanel(); | |
foreach (var ans in answers) | |
{ | |
RadioButton r = new RadioButton() { Content = ans }; | |
panel.Children.Add(r); | |
} | |
TextBox txb = new TextBox() { Text = "てきすとぼっくす" }; | |
panel.Children.Add(txb); | |
Button btn = new Button() { Content = "ぼたん" }; | |
btn.Click += (s, e) => { txb.Text = "くりっく"; }; | |
panel.Children.Add(btn); | |
ProgressBar pgb = new ProgressBar(); | |
pgb.Minimum = 0; | |
pgb.Maximum = 10; | |
pgb.Value = 10; | |
panel.Children.Add(pgb); | |
ContentDialog dlg = new ContentDialog(); | |
dlg.Title = title; | |
dlg.Content = panel; //Contentに表示させたい内容を設定すればよい | |
dlg.PrimaryButtonText = "OK"; | |
dlg.PrimaryButtonClick += (s, e) => | |
{ | |
e.Cancel = true; | |
}; | |
var tend = DateTime.Now.AddSeconds(10); | |
DispatcherTimer timer = new DispatcherTimer(); | |
timer.Interval = TimeSpan.FromSeconds(0.1); | |
timer.Tick += (s, e) => | |
{ | |
pgb.Value = (tend - DateTime.Now).TotalSeconds; | |
if (pgb.Value <= 0) | |
{ | |
dlg.Hide(); | |
} | |
}; | |
timer.Start(); | |
var result = await dlg.ShowAsync(); | |
timer.Stop(); | |
switch (result) | |
{ | |
case ContentDialogResult.Primary: | |
return (string)panel.Children.OfType<RadioButton>().FirstOrDefault(_ => _.IsChecked == true)?.Content; | |
case ContentDialogResult.Secondary: | |
return txb.Text; | |
default: | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment