单击“确定"后的 ContentDialog 延迟 [英] ContentDialog delay after OK clicked

查看:27
本文介绍了单击“确定"后的 ContentDialog 延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个辅助方法,它显示一个 ContentDialog 和一个接受字符串数据的输入框.我的问题是在调用者取回字符串之前单击 OK 大约需要 1 秒(这非常明显).我推测可能是对话框在对话框返回之前淡出默认动画/过渡结束.

I have a helper method that shows an ContentDialog with an input box that accepts string data. My issue is that it takes about 1 second after OK is clicked before the caller gets the string back (and it's very noticeable). I theorized that maybe it was the dialog fading out default animation/transition to end before the dialog returned.

在下面的代码中,在对话框上单击确定"和返回 textBox.Text"之间大约有 1 秒的延迟.

In the code below, there's about a 1 second delay between when OK is clicked on the dialog and when the "return textBox.Text" fires.

    /// <summary>
    /// Shows an simple input box to get a string value in return.
    /// </summary>
    /// <param name="title">The title to show at the top of the dialog.</param>
    /// <param name="message">The message shown directly above the input box.</param>
    /// <param name="defaultValue">A value to prepopulate in the input box if any.</param>
    /// <returns></returns>
    public static async Task<string> ShowInput(string message, string defaultValue, string title)
    {
        var dialog = new ContentDialog
        {
            Title = title
        };

        var panel = new StackPanel();
        panel.Children.Add(new TextBlock { Text = message, TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap });

        var textBox = new TextBox();
        textBox.Text = defaultValue;
        textBox.SelectAll();

        textBox.KeyUp += (o, e) =>
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                dialog.Hide();
            }

            e.Handled = true;
        };

        panel.Children.Add(textBox);

        dialog.Content = panel;
        dialog.PrimaryButtonText = "OK";            
        await dialog.ShowAsync();

        return textBox.Text;
    }

我的问题是:

  1. 我是不是遗漏了一些我应该设置的东西,还是在 ContentDialog 上点击 OK 后出现了这种延迟?

  1. Am I missing something I should be setting or is this delay after clicking OK on the ContentDialog out the out of the box behavior?

如果它是由转换引起的,我可以禁用它吗?

If it is caused by a transition can I disable it?

我正在运行 1809 Build 17763.379.

I am running against 1809 Build 17763.379.

提前致谢.

推荐答案

正如 Pratyay 在他的评论中提到的,金额时间会因设备而异.

As Pratyay mentioned in his comment, the amount of time will vary by device.

但就 await 关键字而言,我相信您遇到的是预期行为.

But as far as the await keyword goes, I believe what you are experiencing is intended behavior.

await 运算符应用于异步方法中的任务,以在方法的执行中插入暂停点,直到等待的任务完成.任务代表正在进行的工作.

The await operator is applied to a task in an asynchronous method to insert a suspension point in the execution of the method until the awaited task completes. The task represents ongoing work.

来源@Microsoft Doc

这意味着您的 ShowInput 函数将在到达 await 关键字后立即返回其 Task 对象.然后在 dialog.ShowAsync(); 返回后,它将继续异步执行 ShowInput 函数并将结果放入 Task 对象中供您检索.

This means that your ShowInput function will return its Task<string> object as soon as it reaches the await keyword. Then after dialog.ShowAsync(); returns it will continue the ShowInput function asynchronously and place the results into the Task object for you to retrieve.

因此,尽管您的 ShowInput 函数应该几乎立即返回.您会注意到 dialog.ShowAsync();return textBox.Text; 之间的延迟.

So although your ShowInput function should return almost immediately. You will notice a delay between dialog.ShowAsync(); and return textBox.Text;.

要记住的另一件事是,当窗口关闭时,在窗口循环完成之前通常会有一些处理(处理资源等).根据您编写代码的方式,您必须等到所有这些都完成后才能获得结果.

The other thing to keep in mind is that when a window closes, there is usually a bit of processing (disposing of resources, etc.) before the window loop finishes. The way your code is written, you will have to wait until all of that finishes before you get your result.

无论如何我可以加快点击确定和等待返回之间的时间吗?

Is there anyway I can speed up the time between when OK is clicked and the await returns?

我认为让您回复的最快方法是不要等待 ContentDialog,而是等待内容可用时发生的信号.

I figure the quickest way to get your response back is to not await the ContentDialog, but rather await a signal that occurs right when the content is available.

public static Task<string> ShowInput(string message, string defaultValue, string title)
{
    var dialog = new ContentDialog { Title = title };

    var panel = new StackPanel();
    panel.Children.Add(new TextBlock { Text = message, TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap });

    var textBox = new TextBox() { Text = defaultValue };
    textBox.SelectAll();

    var signal = new TaskCompletionSource<string>();

    textBox.KeyUp += (o, e) =>
    {
        if (e.Key == Windows.System.VirtualKey.Enter)
        {
            dialog.Hide();
            signal.SetResult(textBox.Text);
        }

        e.Handled = true;
    };
    dialog.PrimaryButtonClick += (o, e) => 
    {
        dialog.Hide();
        signal.SetResult(textBox.Text);
    };

    panel.Children.Add(textBox);

    dialog.Content = panel;
    dialog.PrimaryButtonText = "OK";            
    dialog.ShowAsync();

    return signal.Task;
}

这样做不再需要额外的等待,因为正在创建的任务有最终结果.

Doing it this way no longer needs the extra await, as the Task being created has the final result in it.

这篇关于单击“确定"后的 ContentDialog 延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆