C# 单击按钮时通知主线程

C# 单击按钮时通知主线程,c#,windows-runtime,.net-4.5,C#,Windows Runtime,.net 4.5,我正在尝试创建一个这样的弹出窗口 // Create a Popup var Pop = new Popup() { IsOpen = true }; // Create a StackPanel for the Buttons var SPanel = new StackPanel() { Background = MainGrid.Background }; // Set the comment var TxtBlockComment = new TextBlock { Margin =

我正在尝试创建一个这样的弹出窗口

// Create a Popup
var Pop = new Popup() { IsOpen = true };

// Create a StackPanel for the Buttons
var SPanel = new StackPanel() { Background = MainGrid.Background };

// Set the comment
var TxtBlockComment = new TextBlock { Margin = new Thickness(20, 5, 20, 5), Text = Obj.Comment };

// Set the value
var TxtBoxValue = new TextBox { Name = "MeasureValue" };
TxtBoxValue.KeyUp += (sender, e) => { VerifyKeyUp(sender, Measure); };

// Create the button
var ButtonFill = new Button { Content = Loader.GetString("ButtonAccept"), Margin = new Thickness(20, 5, 20, 5), Style = (Style)App.Current.Resources["TextButtonStyle"] };
ButtonFill.Click += (sender, e) => { Obj.Value = TxtBoxValue.Text; };

// Add the items to the StackPanel
SPanel.Children.Add(TxtBlockComment);
SPanel.Children.Add(TxtBoxValue);
SPanel.Children.Add(ButtonFill);

// Set the child on the popup
Pop.Child = SPanel;
当执行
按钮fill.Click
事件时,我想取消主线程,这样我就可以继续该线程了


但是我该怎么做呢?

在类上放置一个布尔属性,并在事件的开头将该布尔属性设置为true

ButtonFill.Click += (sender, e) => { ButtonClicked = true; Obj.Value = TxtBoxValue.Text; };

我假设您正在询问如何实现类似对话框的行为,类似于
FileOpenPicker.PickSingleFileAsync
。您可以使用
TaskCompletionSource
创建等待的任务:

private Task<string> OpenPopupAsync()
{
    var taskSource = new TaskCompletionSource<string>();

    // Create a Popup
    var Pop = new Popup() { IsOpen = true };

    // Create a StackPanel for the Buttons
    var SPanel = new StackPanel() { Background = MainGrid.Background };

    // Set the comment
    var TxtBlockComment = new TextBlock { Margin = new Thickness(20, 5, 20, 5), Text = Obj.Comment };

    // Set the value
    var TxtBoxValue = new TextBox { Name = "MeasureValue" };
    TxtBoxValue.KeyUp += (sender, e) => { VerifyKeyUp(sender, Measure); };

    // Create the button
    var ButtonFill = new Button { Content = Loader.GetString("ButtonAccept"), Margin = new Thickness(20, 5, 20, 5), Style = (Style)App.Current.Resources["TextButtonStyle"] };
    ButtonFill.Click += (sender, e) => { Pop.IsOpen = false; taskSource.SetResult(TxtBoxValue.Text); };

    // Add the items to the StackPanel
    SPanel.Children.Add(TxtBlockComment);
    SPanel.Children.Add(TxtBoxValue);
    SPanel.Children.Add(ButtonFill);

    // Set the child on the popup
    Pop.Child = SPanel;

    return taskSource.Task;
}

我还建议您查看控件,以获得实现弹出窗口的更简单方法。

您可以尝试使用AutoResetEvent,您的意思是ButtonFill.Clicked事件吗?不清楚应该通知主线程的其他线程在哪里。从您发布的代码来看,它看起来像是单线程的。是的,我指的是
按钮填充。单击
事件;)@Kimi我的意思是如何在方法中通知已抛出
Click
事件?在我看来,我需要使用while循环来检查value@The87Boy我的观点来自MVVM,在MVVM中,布尔值将由INotifyPropertyChanged方法宣布(可以这么说)。将通知绑定到布尔值的任何项。我猜你没有改变我的财产。如果你正在从主线程运行一些东西,那么是的,你需要等待,可能需要线程。在等待更改时睡觉。哦,那么我想我需要调用另一种方法,这正是我所寻找的
string result = await OpenPopupAsync();
// continue execution after the method returns