返回来自uwp用户控件的值



两个按钮的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将在xaml页面中。我可以将可见性绑定到xaml页面中的属性。LeaveStaySaveChangesUserControl中按钮的事件处理程序。如何捕获CanGo作为SaveChangesUserControl的返回值?

像bool canGo =SaveChangesUserControl这样的设置就很好了。

ContentDialog,但不是ContentDialog

您可以使用AutoResetEvent类来执行线程同步。当有信号时,释放单个等待线程后自动复位。

请检查以下代码:

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
}

最新更新