- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我多次按下登录按钮,它会触发消息:“异步操作未正确启动。任何时候只能打开一个 ContentDialog。” (延迟表示应用程序联系服务器以查看用户是否有效所花费的时间。)
如果我使用 MessageDialog 一切正常,但我想使用 ContentDialog 提供的额外定制。
This链接没有帮助。我下面的代码示例展示了我尝试使用它。
XAML:
<Page
x:Class="DuckTracker.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DuckTracker"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Height="20">Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Height="20"></TextBox>
<Button Click="Button_Click" Grid.Row="2" VerticalAlignment="Bottom">Login</Button>
</Grid>
</Page>
代码隐藏:
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
};
await ContentDialogMaker.CreateContentDialogAsync(dialog, true);
}
public async Task<bool> CanLogin()
{
await Task.Delay(1000);
return false;
}
}
}
从上面提到的链接借用的代码:
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
namespace DuckTracker
{
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)
{
await DialogAwaiter.Task;
DialogAwaiter = new TaskCompletionSource<bool>();
}
else ActiveDialog.Hide();
}
ActiveDialog = Dialog;
ActiveDialog.Closed += ActiveDialog_Closed;
await ActiveDialog.ShowAsync();
ActiveDialog.Closed -= ActiveDialog_Closed;
}
public static ContentDialog ActiveDialog;
static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
}
}
我在 Button_Click() 中添加了 DoingStuff,这样可以防止出现错误消息,但我认为一定有更好的方法:
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (DoingStuff == false)
{
DoingStuff = true;
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();
}
}
DoingStuff = false;
}
}
最佳答案
一般情况下,当您启动登录方法时,您需要同时启用您的登录按钮,以避免多次点击。并且您的解决方案也可用,即在显示上一个对话框之前按钮将不起作用。
我创建了新的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_Closing
事件处理程序方法中被清空。并且它可以确保之前的对话框在显示后会被清除。因此,只有一个 ContentDialog
会同时打开。
关于c# - 任何时候只能打开一个 ContentDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486830/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!