C# 任何时候都只能打开一个ContentDialog
如果我多次按我的登录按钮,它会触发消息:“异步操作未正确启动。任何时候只能打开一个ContentDialog。”(延迟表示应用程序与服务器联系以查看用户是否有效所需的时间。) 如果我使用MessageDialog,一切正常,但我想使用ContentDialog提供的额外定制 链接没有帮助。下面的代码示例演示了如何使用它 XAML:C# 任何时候都只能打开一个ContentDialog,c#,xaml,uwp,async-await,C#,Xaml,Uwp,Async Await,如果我多次按我的登录按钮,它会触发消息:“异步操作未正确启动。任何时候只能打开一个ContentDialog。”(延迟表示应用程序与服务器联系以查看用户是否有效所需的时间。) 如果我使用MessageDialog,一切正常,但我想使用ContentDialog提供的额外定制 链接没有帮助。下面的代码示例演示了如何使用它 XAML: 在处理单击时禁用该按钮 private async void Button_Click(object sender, RoutedEventArgs e) {
在处理单击时禁用该按钮
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.IsEnabled = false;
}
… handle button click
if (sender is Button button2)
{
button2.IsEnabled = true;
}
}
在打开下一个对话框之前,请尝试此操作
ContentDialogMaker.ActiveDialog?.Hide();
// Show new Dialog here.
通常,启动登录方法时,需要同时启用登录按钮以避免多次单击。您的解决方案也可用,直到显示上一个对话框,按钮才会工作 我还创建了新的
ContentDialogMaker
。有关更多信息,请参考以下内容
public static class ContentDialogMaker
{
public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
{
if (ActiveDialog != null)
{
if (awaitPreviousDialog)
{
ActiveDialog.Hide();
}
else
{
switch (Info.Status)
{
case AsyncStatus.Started:
Info.Cancel();
break;
case AsyncStatus.Completed:
Info.Close();
break;
case AsyncStatus.Error:
break;
case AsyncStatus.Canceled:
break;
}
}
}
ActiveDialog = Dialog;
ActiveDialog.Closing += ActiveDialog_Closing;
Info = ActiveDialog.ShowAsync();
}
public static IAsyncInfo Info;
private static void ActiveDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
ActiveDialog = null;
}
public static ContentDialog ActiveDialog;
}
唯一的区别是ActiveDialog
在ActiveDialog\u Closing
事件处理程序方法中被清空。它还可以确保在显示之前的对话框后,该对话框将被清除。因此,只有一个内容对话框将同时打开。使用系统;
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace DuckTracker
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
bool canLogin = await CanLogin();
if (canLogin == false)
{
try
{
await AlertWithMessages("Fail", "Could not log in!", "ok");
}
catch (Exception ex)
{
var dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
await dialog.ShowAsync();
}
}
}
public async Task AlertWithMessages(string title, string msg, string confirm)
{
ContentDialog dialog = new ContentDialog
{
Title = title,
Content = msg,
PrimaryButtonText = confirm
};
ContentDialogResult result = await dialog.ShowAsync();
}
public async Task<bool> CanLogin()
{
await Task.Delay(1000);
return false;
}
}
}
使用System.Threading.Tasks;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
名称空间跟踪器
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
bool canLogin=wait canLogin();
if(canLogin==false)
{
尝试
{
使用消息等待警报(“失败”、“无法登录!”、“确定”);
}
捕获(例外情况除外)
{
var dialog=newwindows.UI.Popups.MessageDialog(例如Message,“Error”);
wait dialog.ShowAsync();
}
}
}
公共异步任务AlertWithMessages(字符串标题、字符串消息、字符串确认)
{
ContentDialog=新建ContentDialog
{
头衔,
内容=味精,
PrimaryButtonText=确认
};
ContentDialogResult=Wait dialog.ShowAsync();
}
公共异步任务CanLogin()
{
等待任务。延迟(1000);
返回false;
}
}
}
我以为这已经解决了问题,但还没有解决。我仍然得到相同的错误。你如何在这里附加单击事件,如PrimaryButtonClick?
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
button.IsEnabled = false;
}
… handle button click
if (sender is Button button2)
{
button2.IsEnabled = true;
}
}
ContentDialogMaker.ActiveDialog?.Hide();
// Show new Dialog here.
public static class ContentDialogMaker
{
public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
{
if (ActiveDialog != null)
{
if (awaitPreviousDialog)
{
ActiveDialog.Hide();
}
else
{
switch (Info.Status)
{
case AsyncStatus.Started:
Info.Cancel();
break;
case AsyncStatus.Completed:
Info.Close();
break;
case AsyncStatus.Error:
break;
case AsyncStatus.Canceled:
break;
}
}
}
ActiveDialog = Dialog;
ActiveDialog.Closing += ActiveDialog_Closing;
Info = ActiveDialog.ShowAsync();
}
public static IAsyncInfo Info;
private static void ActiveDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
ActiveDialog = null;
}
public static ContentDialog ActiveDialog;
}
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace DuckTracker
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
bool canLogin = await CanLogin();
if (canLogin == false)
{
try
{
await AlertWithMessages("Fail", "Could not log in!", "ok");
}
catch (Exception ex)
{
var dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error");
await dialog.ShowAsync();
}
}
}
public async Task AlertWithMessages(string title, string msg, string confirm)
{
ContentDialog dialog = new ContentDialog
{
Title = title,
Content = msg,
PrimaryButtonText = confirm
};
ContentDialogResult result = await dialog.ShowAsync();
}
public async Task<bool> CanLogin()
{
await Task.Delay(1000);
return false;
}
}
}