Windows Phone 中的系统 MessageBox 模态对话框
Modal dialog like system MessageBox in Windows Phone
我想制作我自己的 class,以便按照 MessageBox.Show() 的方式在对话框中设置一些值。
我的代码是:
MainPage.xaml.cs
using System;
using System.Windows;
using Microsoft.Phone.Controls;
namespace ModalWindow
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
string result = MyModalBox.GiveMeValue();
MessageBox.Show(result);
}
}
}
MyModalBox.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace ModalWindow
{
public class MyModalBox
{
private static Popup modalBox;
public static string GiveMeValue()
{
TextBox textBox = new TextBox();
textBox.Width = 300;
textBox.Height = 100;
Button okButton = new Button();
okButton.Click += okButton_Click;
okButton.Content = "ok";
okButton.Width = 300;
okButton.Height = 100;
StackPanel stack = new StackPanel();
stack.Background = new SolidColorBrush(Colors.Black);
stack.Width = Application.Current.Host.Content.ActualWidth;
stack.Height = Application.Current.Host.Content.ActualHeight;
stack.HorizontalAlignment = HorizontalAlignment.Center;
stack.VerticalAlignment = VerticalAlignment.Center;
stack.Children.Add(textBox);
stack.Children.Add(okButton);
modalBox = new Popup();
modalBox.Child = stack;
modalBox.IsOpen = true;
// how to change my code to return value only after okButton is cklicked?
return textBox.Text;
}
static void okButton_Click(object sender, RoutedEventArgs e)
{
modalBox.IsOpen = false;
}
}
}
当然在弹出窗口出现之前它没有显示任何结果。单击按钮后如何将我的代码更改为 return 值?可能吗?
提前致谢!
您可以为此使用 TaskComplectionSource。
添加这个:
public class DialogResult<T>
{
private readonly T _result;
private readonly bool _canceled;
public DialogResult(bool isCanceled)
: this(string.Empty, isCanceled)
{
}
public DialogResult(string result, bool canceled = false)
{
_result = result;
_canceled = canceled;
}
public T GetResults()
{
if (HasDialogBeenCanceled())
throw new InvalidOperationException("Dialog has been canceled - no results");
return _result;
}
public bool HasDialogBeenCanceled()
{
return _canceled;
}
}
// inside your dialog control
private TaskCompletionSource<DialogResult<string>> dialogResultAwaiter;
private Button button;
private TextBlock textBox;
private Popup popup;
public async Task<DialogResult<string>> ShowPopup()
{
dialogResultAwaiter = new TaskCompletionSource<DialogResult<string>>();
button.Tapped += (sender, args) => dialogResultAwaiter.SetResult(new DialogResult<string>(textBox.Text, false));
var popup = new Popup();
popup.Closed += PopupOnClosed;
// popup code
popup.IsOpen = true;
return await dialogResultAwaiter.Task;
}
private void PopupOnClosed(object sender, object o)
{
if (dialogResultAwaiter.Task.IsCompleted)
return;
dialogResultAwaiter.SetResult(new DialogResult<string>(true));
}
通过这种方式,您可以创建 "own await" - 当 TaskComplectionSource.SetResult
被调用时,它将 "end"(和 return 结果)。
我想制作我自己的 class,以便按照 MessageBox.Show() 的方式在对话框中设置一些值。
我的代码是:
MainPage.xaml.cs
using System;
using System.Windows;
using Microsoft.Phone.Controls;
namespace ModalWindow
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
string result = MyModalBox.GiveMeValue();
MessageBox.Show(result);
}
}
}
MyModalBox.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace ModalWindow
{
public class MyModalBox
{
private static Popup modalBox;
public static string GiveMeValue()
{
TextBox textBox = new TextBox();
textBox.Width = 300;
textBox.Height = 100;
Button okButton = new Button();
okButton.Click += okButton_Click;
okButton.Content = "ok";
okButton.Width = 300;
okButton.Height = 100;
StackPanel stack = new StackPanel();
stack.Background = new SolidColorBrush(Colors.Black);
stack.Width = Application.Current.Host.Content.ActualWidth;
stack.Height = Application.Current.Host.Content.ActualHeight;
stack.HorizontalAlignment = HorizontalAlignment.Center;
stack.VerticalAlignment = VerticalAlignment.Center;
stack.Children.Add(textBox);
stack.Children.Add(okButton);
modalBox = new Popup();
modalBox.Child = stack;
modalBox.IsOpen = true;
// how to change my code to return value only after okButton is cklicked?
return textBox.Text;
}
static void okButton_Click(object sender, RoutedEventArgs e)
{
modalBox.IsOpen = false;
}
}
}
当然在弹出窗口出现之前它没有显示任何结果。单击按钮后如何将我的代码更改为 return 值?可能吗?
提前致谢!
您可以为此使用 TaskComplectionSource。
添加这个:
public class DialogResult<T>
{
private readonly T _result;
private readonly bool _canceled;
public DialogResult(bool isCanceled)
: this(string.Empty, isCanceled)
{
}
public DialogResult(string result, bool canceled = false)
{
_result = result;
_canceled = canceled;
}
public T GetResults()
{
if (HasDialogBeenCanceled())
throw new InvalidOperationException("Dialog has been canceled - no results");
return _result;
}
public bool HasDialogBeenCanceled()
{
return _canceled;
}
}
// inside your dialog control
private TaskCompletionSource<DialogResult<string>> dialogResultAwaiter;
private Button button;
private TextBlock textBox;
private Popup popup;
public async Task<DialogResult<string>> ShowPopup()
{
dialogResultAwaiter = new TaskCompletionSource<DialogResult<string>>();
button.Tapped += (sender, args) => dialogResultAwaiter.SetResult(new DialogResult<string>(textBox.Text, false));
var popup = new Popup();
popup.Closed += PopupOnClosed;
// popup code
popup.IsOpen = true;
return await dialogResultAwaiter.Task;
}
private void PopupOnClosed(object sender, object o)
{
if (dialogResultAwaiter.Task.IsCompleted)
return;
dialogResultAwaiter.SetResult(new DialogResult<string>(true));
}
通过这种方式,您可以创建 "own await" - 当 TaskComplectionSource.SetResult
被调用时,它将 "end"(和 return 结果)。