在uwp

时间:2017-09-04 15:59:07

标签: asynchronous uwp controls

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

5 个答案:

答案 0 :(得分:1)

我创建了一个复制JavaScript var stream = recognizeMic({ token: token, objectMode: false, // send objects instead of text extractResults: false, // convert {results: [{alternatives:[...]}], result_index: 0} to {alternatives: [...], index: 0} format: false // optional - performs basic formatting on the results such as capitals an periods }); stream.setEncoding('utf8'); stream.on('data', (data) => { console.log(data); var txt = JSON.stringify(data); console.log(txt); if (txt === "example") { console.log("Success"); window.open("https://www.example.com") } }); stream.on('error', function(err) { console.log(err); }); //document.querySelector('#stop').onclick = stream.stop.bind(stream); }).catch(function(error) { console.log(error); }); } 函数功能的函数。它创建一个ContentDialog并将其添加到队列中。事件处理程序确保当Dialog关闭时,它将自己从队列中删除,并为队列中的下一个Dialog调用Alert(),假设有一个。

ShowAsync()

我将静态函数放在App.xaml.cs中(以及队列和对话框关闭的事件处理程序),所以你可以像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.Alert("Hello world")添加参数以填充ContentDialog的不同属性。

答案 1 :(得分:0)

您可以改为使用消息对话框。

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

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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

一次只能显示一个ContentDialog。

如果显示一个ContentDialog,我们就无法显示另一个ContentDialog。当您在ShowAsync的{​​{1}}事件中添加LostFocus方法并使用点击按钮时,它会同时显示两个TextBox

如果可以显示ContentDialog,您应该能够添加bool字段以保存到状态。

例如:

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中。

答案 4 :(得分:0)

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

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

希望这会有所帮助..

相关问题