Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone中类似于系统消息框的模式对话框_C#_Windows Phone 8 - Fatal编程技术网

C# Windows Phone中类似于系统消息框的模式对话框

C# Windows Phone中类似于系统消息框的模式对话框,c#,windows-phone-8,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()

我想用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; 
        }
    }
}
当然,在弹出窗口出现之前,它不会显示任何结果。单击按钮后,如何将代码更改为仅返回值?可能吗


提前谢谢

您可以使用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));
    }
公共类对话框结果
{
私有只读T_结果;
私人只读bool\u已取消;
公共对话框结果(bool已取消)
:此(string.Empty,已取消)
{
}
公共对话框结果(字符串结果,bool cancelled=false)
{
_结果=结果;
_取消=取消;
}
公共T GetResults()
{
如果(HasDialogBeenCanceled())
抛出新的InvalidOperationException(“对话框已取消-无结果”);
返回结果;
}
公共图书馆已被取消()
{
退货(已取消);;
}
}
//在对话框控件中
私有TaskCompletionSource对话框ResultaWater;
私人按钮;
私有文本块文本框;
私人弹出窗口;
公共异步任务ShowPopup()
{
DialogResultaWater=new TaskCompletionSource();
点击按钮+=(发送者,参数)=>DialogResultAwer.SetResult(新的DialogResult(textBox.Text,false));
var popup=newpopup();
popup.Closed+=PopupOnClosed;
//弹出代码
popup.IsOpen=true;
返回等待对话框resultaweer.Task;
}
私有void PopupOnClosed(对象发送方,对象o)
{
如果(DialogResultAwer.Task.IsCompleted)
回来
SetResult(新的DialogResult(true));
}

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

如何调用方法ShowPopup()?您需要等待它,并且调用方方法应标记为async(var results=wait ShowPopup();)-阅读有关async/wait关键字的内容。已阅读。这似乎有效,但告诉鳟鱼我不明白怎么做:-)。谢谢!据我所知,不可能使用像MyModalBox.ShowPopup()这样的静态非异步方法。我总是需要添加异步方法来使用我的类?创建静态异步任务方法非常有效。只需确保您不共享TaskCompletionSource—每个ShowPopup都应该有自己的TaskCompletionSource。顺便说一句,如果我帮忙,你可以投票接受。