Asynchronous uwp中的队列会话
是否可以对ContentDialog进行排队,并在关闭另一个ContentDialog后显示它? 我用这个来查找ContentDialogAsynchronous uwp中的队列会话,asynchronous,uwp,controls,Asynchronous,Uwp,Controls,是否可以对ContentDialog进行排队,并在关闭另一个ContentDialog后显示它? 我用这个来查找ContentDialog var popups = VisualTreeHelper.GetOpenPopups(Window.Current); int i = 0; foreach (var item in popups) { if (item.Child is ContentDialog)
var popups = VisualTreeHelper.GetOpenPopups(Window.Current);
int i = 0;
foreach (var item in popups)
{
if (item.Child is ContentDialog)
i++;
}
if (i == 0)
{
//show ContentDialog
}
else
// add to my listOfContentDialog
但如果我试图一次打开更多ContentDialog,它会抛出一个异常,说明操作启动不正确
更新
根据Jayden Gu-MSFT我的工作代码
private List<string> testowa_lista = new List<string>();
private async void Komunikat_siatki(string v)
{
if(mozeWyskoczyc)
{
mozeWyskoczyc = false;
//my internal code to generate ContentDialog using v string
testowa_lista.Remove(v);
mozeWyskoczyc = true;
if (testowa_lista.Count > 0)
{
var i = testowa_lista.First();
Komunikat_siatki(i);
}
}
else
{
testowa_lista.Add(v);
}
}
private List testowa_lista=new List();
私有异步void Komunikat_siatki(字符串v)
{
if(mozeWyskoczyc)
{
mozeWyskoczyc=false;
//使用v字符串生成ContentDialog的内部代码
删除(v);
mozeWyskoczyc=true;
如果(testowa_lista.Count>0)
{
var i=testowa_lista.First();
Komunikat_siatki(一);
}
}
其他的
{
testowa_lista.Add(v);
}
}
您可以使用消息对话框
await new MessageDialog("First").ShowAsync();
await new MessageDialog("Second").ShowAsync();
您可以改为使用消息对话框
await new MessageDialog("First").ShowAsync();
await new MessageDialog("Second").ShowAsync();
您可以将它们存储在某个集合中,然后逐个显示:
List<ContentDialog> dialogs = new List<ContentDialog>()
{
contentDialog1,
contentDialog2,
contentDialog3
};
foreach (ContentDialog dialog in dialogs)
{
await dialog.ShowAsync();
}
列表对话框=新建列表()
{
内容对话框1,
内容对话框2,
内容对话框3
};
foreach(对话框中的ContentDialog)
{
wait dialog.ShowAsync();
}
您可以将它们存储在某个集合中,然后逐个显示:
List<ContentDialog> dialogs = new List<ContentDialog>()
{
contentDialog1,
contentDialog2,
contentDialog3
};
foreach (ContentDialog dialog in dialogs)
{
await dialog.ShowAsync();
}
列表对话框=新建列表()
{
内容对话框1,
内容对话框2,
内容对话框3
};
foreach(对话框中的ContentDialog)
{
wait dialog.ShowAsync();
}
一次只能显示一个ContentDialog
如果显示一个ContentDialog
,则无法显示另一个ContentDialog
。当您在文本框的LostFocus
事件中添加showsync
方法并使用点击按钮时,它将同时显示两个ContentDialog
如果可以显示ContentDialog
,您应该能够添加一个bool字段以保存到的状态
例如:
private async void Text_LostFocus(object sender, RoutedEventArgs e)
{
if (dialogCanShow)
{
dialogCanShow = false;
var textBox= sender as TextBox;
var contentDialog = new ContentDialog();
contentDialog.Title = textBox.Name;
contentDialog.CloseButtonText = "Close";
await contentDialog.ShowAsync();
dialogCanShow = true;
}
}
当我们关闭ContentDialog
时,光标将显示在当前文本框中
一次只能显示一个ContentDialog
如果显示一个ContentDialog
,则无法显示另一个ContentDialog
。当您在文本框的LostFocus
事件中添加showsync
方法并使用点击按钮时,它将同时显示两个ContentDialog
如果可以显示ContentDialog
,您应该能够添加一个bool字段以保存到的状态
例如:
private async void Text_LostFocus(object sender, RoutedEventArgs e)
{
if (dialogCanShow)
{
dialogCanShow = false;
var textBox= sender as TextBox;
var contentDialog = new ContentDialog();
contentDialog.Title = textBox.Name;
contentDialog.CloseButtonText = "Close";
await contentDialog.ShowAsync();
dialogCanShow = true;
}
}
当我们关闭内容对话框
时,光标将显示在当前的文本框中
,因为您需要对多个内容对话框进行排队,您可以尝试以下操作:
int count = 1; //count is used to simply to have dynamic content in the dialogs
private async void startCreatingDialog()
{
var result = await createDialog(count.ToString()).ShowAsync();
if (result == ContentDialogResult.Primary)
{
//create a new dialog based on user's input
count++;
startCreatingDialog();
}
}
private ContentDialog createDialog(string content)
{
ContentDialog contentDialog = new ContentDialog()
{
Content = content,
//clicking on the primarybutton will create the next ContentDialog
PrimaryButtonText = "Show another dialog",
SecondaryButtonText = "Cancel"
};
return contentDialog;
}
要开始显示对话框队列,您需要调用startCreatingDialog()
,其余的将根据用户的选择进行处理
希望这对您有所帮助。由于您需要对多个内容对话框进行排队,您可以尝试以下方法:
int count = 1; //count is used to simply to have dynamic content in the dialogs
private async void startCreatingDialog()
{
var result = await createDialog(count.ToString()).ShowAsync();
if (result == ContentDialogResult.Primary)
{
//create a new dialog based on user's input
count++;
startCreatingDialog();
}
}
private ContentDialog createDialog(string content)
{
ContentDialog contentDialog = new ContentDialog()
{
Content = content,
//clicking on the primarybutton will create the next ContentDialog
PrimaryButtonText = "Show another dialog",
SecondaryButtonText = "Cancel"
};
return contentDialog;
}
要开始显示对话框队列,您需要调用startCreatingDialog()
,其余的将根据用户的选择进行处理
希望这能有所帮助。我创建了一个复制JavaScriptAlert()
函数功能的函数。它创建一个ContentDialog并将其添加到队列中。事件处理程序确保当对话框关闭时,它将自己从队列中移除,并为队列中的下一个对话框调用showsync()
,假设存在对话框
密封部分类应用程序:应用程序
{
...
私有静态列表对话框队列{get;}=new List();
公共静态异步无效警报(字符串文本)
{
var Dialog=newcontentdialog()
{
标题=文本,
CloseButtonText=“确定”
...
};
App.DialogQueue.Add(对话框);
//为对话框关闭时添加事件处理程序:
Dialog.Closed+=Dialog\u Closed;
if(App.DialogQueue.Count==1)//队列中只有一个
{
wait Dialog.ShowAsync();
}
}
//对话框关闭时的事件处理程序:
私有静态异步无效对话框\u已关闭(ContentDialog发送方,ContentDialogClosedEventArgs args)
{
App.DialogQueue.Remove(发送方);
如果(App.DialogQueue.Count>0)
{
等待App.DialogQueue[0].ShowAsync();
}
}
}
我将静态函数放在App.xaml.cs中(以及队列和对话框关闭的事件处理程序),因此您可以像App.Alert(“Hello world”)
一样调用它,尽管您可以将它放在自己的类中。当然,您可以将参数添加到Alert()
以填充ContentDialog的不同属性。我创建了一个函数,它复制了JavaScriptAlert()
函数的功能。它创建一个ContentDialog并将其添加到队列中。事件处理程序确保当对话框关闭时,它将自己从队列中移除,并为队列中的下一个对话框调用showsync()
,假设存在对话框
密封部分类应用程序:应用程序
{
...
私有静态列表对话框队列{get;}=new List();
公共静态异步无效警报(字符串文本)
{
var Dialog=newcontentdialog()
{
标题=文本,
CloseButtonText=“确定”
...
};
App.DialogQueue.Add(对话框);
//为对话框关闭时添加事件处理程序:
Dialog.Closed+=Dialog\u Closed;
if(App.DialogQueue.Count==1)//队列中只有一个
{
wait Dialog.ShowAsync();
}
}
//对话框关闭时的事件处理程序:
私有静态异步无效对话框\u已关闭(ContentDialog发送方,ContentDialogClosedEventArgs args)
{
App.DialogQueue.Remove(发送方);
如果(App.DialogQueue.Count>0)
{
等待App.DialogQueue[0].ShowAsync();
}
}
}