在uwp中排队ContentDialog

在uwp中排队ContentDialog

问题描述:

可以对ContentDialog进行排队并在另一个关闭后显示它? 我用这个来找到ContentDialog在uwp中排队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的同时也抛出操作不当开始异常。

UPDATE 根据杰登谷 - 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); 
     } 
    } 
+0

我可以知道你想排队多少个ContentDialog?我会尽力根据那个提供一个解决方案.. – Pratyay

+0

ContentDialogs的数量是动态的(最多5) – kdn29

+0

还有另一种方法描述在https://stackoverflow.com/questions/33018346/only-a-single-contentdialog-可以在任何时间打开任何时候打开 - 错误 - 开 - 另一个/ 47986634#47986634 –

只有一个ContentDialog可以同时显示。

如果一个ContentDialog所示,我们无法向另一个ContentDialog。当您在TextBoxLostFocus事件中添加ShowAsync方法并使用“点击”按钮时,它将同时显示两个ContentDialog

您应该能够添加一个布尔字段保存到,如果可以证明的ContentDialog状态。

例如:

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,光标会显示当前TextBox

+0

部分是我想要的...它可以防止打开多个然后一个ContentDialog但不创建队列。 – kdn29

+0

@ kdn29看看https://stackoverflow.com/questions/33018346/only-a-single-contentdialog-can-be-open-at-any-time-error-while-opening-anoth/47986634#47986634 –

你可以使用一个消息,而不是对话框。

await new MessageDialog("First").ShowAsync(); 

await new MessageDialog("Second").ShowAsync(); 
+0

不,它不是我所需要的 – kdn29

您可以将它们存储在某些集合,然后向他们展示一个接一个:

List<ContentDialog> dialogs = new List<ContentDialog>() 
{ 
    contentDialog1, 
    contentDialog2, 
    contentDialog3 
}; 

foreach (ContentDialog dialog in dialogs) 
{ 
    await dialog.ShowAsync(); 
} 
+0

在UWP中,只有一个ContentDialog可以一次打开。你的代码抛出异常。 – kdn29

+0

你试过了吗?它适用于我,因为一旦一个对话框关闭,执行'foreach'循环就会继续。 –

+0

对不起,但它不工作。只有一个ContentDialog可以打开一次 – kdn29

既然你需要排队多个内容的对话框,你可以试试这个:

  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()和休息对话框队列将根据用户的选择来处理..

希望这可以帮助..