如何暂停,而不会阻塞UI [英] How to Pause without Blocking the UI

查看:163
本文介绍了如何暂停,而不会阻塞UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这将导致手机一旦一个按钮被点击振动的单击事件。这通常顺利,但有时震动犯规停止,直到我完全关闭应用程序。我想给应用程序的时间来完成其振动,然后继续该方法的休息,但我不希望在所有阻止用户界面。我怎么可能做到这一点。

I have a click event which causes the phone to vibrate once a button is clicked. This generally works great, except sometimes the vibration doesnt stop until I completely close the application. I would like to give the application time to complete its vibration and then continue with the rest of the method, but I do not want to block the UI at all. How might I accomplish this

MainPage.xaml.cs中

MainPage.xaml.cs

void newButton_Click(object sender, EventArgs e)
{
    //Button vibration
    if (Settings.EnableVibration.Value)  //boolean flag to tell whether to vibrate or not
    {
        VibrateController.Default.Start(TimeSpan.FromMilliseconds(100));
        //place vibration stop here?
    }        

    this.NavigationService.Navigate(new uri("/NewPage.xaml", UriKind.Relate)); 
}



我已经尝试VibrationController.Default.Stop();但这完全消除振动都在一起。有没有办法简单地等到振动已完成,然后导航到一个新的页面,或做任何其他操作方法应该执行?在这个实施或其他任何建议或建议步骤建议?

I have already tried VibrationController.Default.Stop(); but this completely eliminates the vibration all together. Is there a way to simply wait until after the vibration has completed to then navigate to a new page, or do whatever other action the method should perform? Any reccomendations or advice on this implementation or other suggestions?

推荐答案

您可以使用异步性,以防止阻塞UI。而不是实际阻塞UI线程,你需要安排一个动作再次为100ms,从现在​​情况发生。添加continutation到 Task.Delay 电话可以做到这一点:

You can use asynchrony to prevent blocking the UI. Rather than actually blocking the UI thread you need to schedule an action to happen again 100ms from now. Adding a continutation to a Task.Delay call can do just that:

void newButton_Click(object sender, EventArgs e)
{
    Action navigate = () =>
        this.NavigationService.Navigate(new uri("/NewPage.xaml", UriKind.Relate));
    if (Settings.EnableVibration.Value)  //boolean flag to tell whether to vibrate or not
    {
        VibrateController.Default.Start();
        Task.Delay(100).ContinueWith(t =>
        {
            VibrationController.Default.Stop();
            navigate();
        });
    }
    else
        navigate();
}

这篇关于如何暂停,而不会阻塞UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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