两个按钮的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页面中的属性。Leave
和Stay
是SaveChangesUserControl
中按钮的事件处理程序。如何捕获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
}