【问题标题】:Queuing ContentDialog in uwp在 uwp 中排队 ContentDialog
【发布时间】:2018-03-17 06:32:59
【问题描述】:

是否可以对 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);
        }
    }

【问题讨论】:

  • 我可以知道您要排队多少个 ContentDialogs 吗?我会尝试据此提供解决方案..
  • ContentDialogs 的数量是动态的(最多 5 个)
  • stackoverflow.com/questions/33018346/…中描述了另一种方法

标签: asynchronous uwp controls


【解决方案1】:

我创建了一个函数来复制 JavaScript Alert() 函数的功能。它创建一个 ContentDialog 并将其添加到队列中。事件处理程序确保当 Dialog 关闭时,它将自己从队列中移除,并为队列中的下一个 Dialog 调用 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"),尽管您可以将其放入自己的类中。当然,你也可以给Alert()添加参数来填充ContentDialog的不同属性。

【讨论】:

    【解决方案2】:

    一次只能显示一个 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上。

    【讨论】:

    【解决方案3】:

    您可以改用消息对话框。

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

    【讨论】:

    • 不,这不是我需要的
    【解决方案4】:

    您可以将它们存储在某个集合中,然后一个接一个地显示它们:

    List<ContentDialog> dialogs = new List<ContentDialog>()
    {
        contentDialog1,
        contentDialog2,
        contentDialog3
    };
    
    foreach (ContentDialog dialog in dialogs)
    {
        await dialog.ShowAsync();
    }
    

    【讨论】:

    • 在 UWP 中,一次只能打开一个 ContentDialog。您的代码抛出异常。
    • 你试过了吗?它对我有用,因为一旦关闭一个对话框,foreach 循环的执行将继续。
    • 抱歉,它不起作用。抛出一次只能打开一个 ContentDialog 的异常
    • "在 kdnmed.exe 中发生了“System.Runtime.InteropServices.COMException”类型的异常,但未在用户代码 WinRT 信息中处理:W danym momencie tylko jeden element ContentDialog może być otwarty。Operacja asynchroniczna nie została poprawnie rozpoczęta."
    • 这很奇怪,我也不例外...你能把整个代码贴出来让我看看吗?
    【解决方案5】:

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

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

    希望这会有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      相关资源
      最近更新 更多