简体   繁体   English

如何在Windows Phone中使功能等待特定时间?

[英]How to make a function to wait for a specific time in Windows phone?

I am building a Windows phone application with lots of animations. 我正在构建带有许多动画的Windows Phone应用程序。 What I need is that when the application starts, it should be done finishing the first animation, say myS() is the name of the storyboard. 我需要的是,当应用程序启动时,应该完成第一个动画,比如说myS()是情节提要的名称。 Only after it is done with the animation, a textblock should appear after 3 seconds. 仅在完成动画后,文本块应在3秒钟后出现。 What methods should I use to make the display of textbox wait for the Storyboard to finish? 我应该使用什么方法使文本框的显示等待情节提要完成? What I tried is using Thread.Sleeps(10000) but that doesn't work. 我尝试的是使用Thread.Sleeps(10000),但这不起作用。 This is the code - 这是代码-

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        c1.Visibility = Visibility.Collapsed; //c1 is the name of the textbox
        myS.Begin();
        canDisp();
    }

    private void e1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
    {
        myS1.Begin();
    }

    private void e1_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        myS2.Begin();
    }
    void canDisp()
    {
        c1.Visibility = Visibility.Visible;
    }}

After the myS.Begin() is done executing, I want the program to wait for 3 seconds & then execute the canDisp() method, how can I achieve that? myS.Begin()完成执行后,我希望程序等待3秒钟,然后执行canDisp()方法,如何实现?

If your animation is a Storyboard then it has a Completed event ( MSDN link ) for just this purpose. 如果您的动画是Storyboard则为此有一个Completed事件( MSDN链接 )。

Your call to Thread.Sleep() was probably being run on the same thread as the animation and so was stopping the animation from running while it was sleeping. 您对Thread.Sleep()调用可能是在与动画相同的线程上运行的,因此在动画休眠时停止了动画的运行。
If you really want to go down this route then you'll need to move your sleep call to a different thread. 如果您真的想走这条路,那么您需要将睡眠呼叫移到另一个线程。


If you really want to use threads, a simple way to do it is: 如果您真的想使用线程,一个简单的方法是:

System.Threading.ThreadPool.QueueUserWorkItem(obj =>
    {
        System.Threading.Thread.Sleep(5000);

        Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show("after delay");
            });
    });

I am using a windows store app where System.Threading.ThreadPool.QueueUserWorkItem is not available.So I did like this and not working as expected 我正在使用Windows Store应用程序,其中System.Threading.ThreadPool.QueueUserWorkItem不可用。所以我确实这样做了,并且无法按预期工作

  await Windows.System.Threading.ThreadPool.RunAsync(async (s) =>
        {

            await Task.Delay(1);
            await MainViewModel.Instance.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
            {
            MessageBox.Show("after delay");
             });
             }, Windows.System.Threading.WorkItemPriority.High);

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

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