Return 来自 uwp 用户控件的值

Return a value from a uwp usercontrol

一个 UWP UserControl 有 2 个按钮

public sealed partial class SaveChangesUserControl : UserControl
{
    public bool CanGo { get; set; }

    public SaveChangesUserControl()
    {
        InitializeComponent();
    }

    private void Leave(object sender, EventArgs e)
    {
        CanGo = true;
    }

    private void Stay(object sender, EventArgs e)
    {
        CanGo = false;
    }
}

SaveChangesUserControl 将在 xaml 页面中。我可以将可见性绑定到 xaml 页面中的 属性。 LeaveStaySaveChangesUserControl 中按钮的事件处理程序。如何将 CanGo 捕获为 SaveChangesUserControl 的 return 值?

像 bool canGo = SaveChangesUserControl 这样的东西会很好。

ContentDialog,但不像 ContentDialog

您可以使用 AutoResetEvent class 进行线程同步。发出信号时,释放单个等待线程后自动重置。

请检查以下代码:

SaveChangesUserControl.xaml.cs

private static AutoResetEvent Locker = new AutoResetEvent(false);

public async Task<bool> ShowAsync()
{
    Visibility = Visibility.Visible;
    await Task.Run(() => {
        Locker.WaitOne();  //Wait a singal
    });
    return CanGo;
}
private void Leave(object sender, RoutedEventArgs e)
{
    Visibility = Visibility.Collapsed;
    CanGo = true;
    Locker.Set();  //Release a singal
}

private void Stay(object sender, RoutedEventArgs e)
{
    Visibility = Visibility.Collapsed;
    CanGo = false;
    Locker.Set();  //Release a singal
}

MainPage.xaml.cs

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var t = await myUserControl.ShowAsync();  //Call the method, you could get a return value after you click on user control
}