0

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

// 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;

我想在ButtonFill.Click事件执行后通知主线程,这样我就可以继续该线程

但我该怎么做呢?

4

2 回答 2

2

我将假设您正在询问如何实现类似对话的行为,类似于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;
}

简而言之:

  • 你创建一个实例TaskCompletionSource
  • 您将其Task属性作为等待任务返回
  • 当您希望调用方法继续时,您调用SetResult

在调用方法中,您只需await让方法在继续执行之前返回:

string result = await OpenPopupAsync();
// continue execution after the method returns

我还建议您查看Callisto 的 Flyout控件,以更简单的方式实现弹出窗口。

于 2013-01-10T06:00:29.540 回答
0

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

ButtonFill.Click += (sender, e) => { ButtonClicked = true; Obj.Value = TxtBoxValue.Text; };
于 2013-01-09T11:09:32.533 回答