在 uwp 中排队 ContentDialog

Queuing ContentDialog in uwp

是否可以将 ContentDialog 排队并在另一个关闭后显示? 我用它来找到 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);
        }
    }

您可以改用消息对话框。

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();
}

一次只能显示一个ContentDialog。

如果显示一个 ContentDialog,我们不能显示另一个 ContentDialog。当你在TextBoxLostFocus事件中添加ShowAsync方法并使用Tap按钮时,它会同时显示两个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

由于您需要对多个内容对话框进行排队,您可以试试这个:

            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(),其余的将根据用户的选择进行处理。

希望这对您有所帮助..

我创建了一个函数来复制 JavaScript Alert() 函数的功能。它创建一个 ContentDialog 并将其添加到队列中。事件处理程序确保当对话框关闭时,它将自己从队列中删除并为队列中的下一个对话框调用 ShowAsync(),假设有一个。

sealed partial class App : Application
{
    ...
    private static List<ContentDialog> DialogQueue { get; } = new List<ContentDialog>();

    public static async void Alert(string text)
    {
        var Dialog = new ContentDialog()
        {
            Title = text,
            CloseButtonText = "OK"
            ...
        };
        App.DialogQueue.Add(Dialog);

        // Add event handler for when dialog closed:
        Dialog.Closed += Dialog_Closed;

        if (App.DialogQueue.Count == 1) // Only one in queue
        {
            await Dialog.ShowAsync();
        }
    }
    // Event handler for when dialog closed:
    private static async void Dialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
    {
        App.DialogQueue.Remove(sender);
        if (App.DialogQueue.Count > 0)
        {
            await App.DialogQueue[0].ShowAsync();
        }
    }
}

我已将静态函数放在 App.xaml.cs 中(连同队列和对话框关闭事件处理程序),因此您可以像 App.Alert("Hello world") 一样调用它,尽管您可以将它放在自己的 class。当然,你可以给Alert()添加参数来填充ContentDialog的不同属性。