模态对话框,如Windows Phone中的系统MessageBox

时间:2015-01-13 16:57:55

标签: c# windows-phone-8

我想创建自己的类,以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; 
        }
    }
}

当然它显示没有出现弹出窗口的结果。如何在单击按钮后更改我的代码以返回值onli?有可能吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以使用TaskCompletionSource。

添加:

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));
    }

通过这种方式,您可以创建“自己的等待” - 在调用TaskComplectionSource.SetResult时将“结束”(并返回结果)。