简体   繁体   English

按钮上的Windows Phone 7开发延迟

[英]Windows Phone 7 Development Delay on Buttons

I have created a button in C# for Windows phone 7 development. 我在C#中为Windows Phone 7开发创建了一个按钮。 It contains an animation and when clicked I want the animation to be shown, and then for the button to navigate to the next page. 它包含一个动画,点击后我想要显示动画,然后按钮导航到下一页。

Using Thread.Sleep(4000) just crashes the application and I was wondering how I go about this. 使用Thread.Sleep(4000)只是崩溃应用程序,我想知道我是如何做到这一点。

Is there a way to delay the navigate code? 有没有办法延迟导航代码?

private void batnballBtn_Click(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
}

Presumably the animation is a Storyboard . 据推测,动画是一个Storyboard You can navigate within the Storyboard's Completed event. 您可以在Storyboard的Completed事件中导航。

myStoryboard.Completed += (s, e) =>
{
    myStoryboard.Stop();        
    NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
};

This way you don't need to predict how long the animation will take, making the code more reusable, and you don't have to worry about dealing with multiple threads manually. 这样您就不需要预测动画将花费多长时间,使代码更具可重用性,并且您不必担心手动处理多个线程。

Use a thread for that: 使用一个线程:

http://techkn0w.wordpress.com/2012/04/18/using-a-background-thread-in-windows-phone-7-wp7/ http://techkn0w.wordpress.com/2012/04/18/using-a-background-thread-in-windows-phone-7-wp7/

You're stopping the UI thread that's why the button is freeze. 您正在停止UI线程,这就是按钮冻结的原因。

There is only one thread responsible for managing the UI, so, if you block it when doing lengthy operations or making it sleep the UI process management is freezed until that work has finished. 只有一个线程负责管理UI,因此,如果在执行冗长的操作或使其休眠时阻止它,则UI流程管理将被冻结,直到该工作完成。

Something like this: 像这样的东西:

private void batnballBtn_Click(object sender, RoutedEventArgs e)
{
        ThreadPool.QueueUserWorkItem((WaitCallback)delegate(object state)
        {
            Thread.Sleep(4000);

            this.Dispatcher.BeginInvoke((Action)delegate
            {
                NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
            });
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM