How to delay in Storyboard of blend for windows phone application

119 Views Asked by At

I am developing a windows phone game in expression blend that generate random numbers. The problem is that i want to start my storyboard after certain seconds the page loaded. I have tried implementing Timespan.FromSeconds(5) but it didn't work. I want the storyboard to be played when my timer stops. Also i have tried to start storyboard in "if" condition of dispathertimer_Tick method but it too didn't work. Please suggest me alternative for that.

 DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
 public MainPage()
    {
        InitializeComponent();

        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
        this.Loaded += LayoutRoot_Loaded;
    }

    int count = 5;
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {          
        count--;
        if (count < 0)
        {
            dispatcherTimer.Stop();
            timer.Text = "Time Over";
            count = 5;

        }
        else
        {
            timer.Text = count.ToString();
        }

    }

    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
        expr.Text = num.Next(100).ToString();
        rectangle.BeginTime = TimeSpan.FromSeconds(5);
    }
3

There are 3 best solutions below

0
Łukasz Rejman On BEST ANSWER

Storyboard has a property BeginTime. Just set it to 5 seconds.

In XAML:

<Storyboard BeginTime="00:00:05" />

or C#:

myStoryboard.BeginTime = TimeSpan.FromSeconds(5);
2
Sanka Bulathgama On

Why dont you use threads.

System.Threading.Thread.Sleep(5000);

or

// Sleep for 5 seconds
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 5));
1
gayan1991 On

One of the way you could do is, you could make another page as your start up page and do all those things. another way is to do those things like after 5 seconds you insert all those things to layout root dynamically...!

Thread.sleep works if you do not have any progress to show. but in a tricky way you could stop sleep every second and show progress...!