简体   繁体   English

从 uwp 用户控件返回一个值

[英]Return a value from a uwp usercontrol

A UWP UserControl with 2 buttons带有 2 个按钮的UWP UserControl

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 will be in a xaml page. SaveChangesUserControl将在 xaml 页面中。 I can bind the visibility to a property in the xaml page.我可以将可见性绑定到 xaml 页面中的一个属性。 Leave and Stay are event handlers for buttons in SaveChangesUserControl . LeaveStaySaveChangesUserControl中按钮的事件处理程序。 How do I capture CanGo as the return value of SaveChangesUserControl ?如何捕获CanGo作为SaveChangesUserControl的返回值?

Something like bool canGo = SaveChangesUserControl would be nice.像 bool canGo = SaveChangesUserControl这样的东西会很好。

Like a ContentDialog , but not a ContentDialogContentDialog ,但不是ContentDialog

A UWP UserControl with 2 buttons带有 2 个按钮的UWP UserControl

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 will be in a xaml page. SaveChangesUserControl将位于 xaml 页面中。 I can bind the visibility to a property in the xaml page.我可以将可见性绑定到 xaml 页面中的属性。 Leave and Stay are event handlers for buttons in SaveChangesUserControl . LeaveStaySaveChangesUserControl中按钮的事件处理程序。 How do I capture CanGo as the return value of SaveChangesUserControl ?如何捕获CanGo作为SaveChangesUserControl的返回值?

Something like bool canGo = SaveChangesUserControl would be nice.像 bool canGo = SaveChangesUserControl这样的东西会很好。

Like a ContentDialog , but not a ContentDialogContentDialog ,但不是ContentDialog

You could use AutoResetEvent class to do a thread synchronization.您可以使用AutoResetEvent class 进行线程同步。 When signaled, reset automatically after releasing a single waiting thread.发出信号时,释放单个等待线程后自动重置。

Please check the following code:请检查以下代码:

SaveChangesUserControl.xaml.cs 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 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM