Search code examples
c#windows-phone-7navigationdelaysleep

Windows 7 Phone - Putting a delay in screen navigation


Hi I am trying to delay the screen navigation. After loading MainPage screen, I want to navigate to MainMenu screen after 3 seconds. I have written the following code for that.

public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Default_Loaded);         
    }

    private void Default_Loaded(object sender, RoutedEventArgs e)
    {
       Thread.Sleep(3000);
       this.NavigationService.Navigate(new Uri("/MyProject;component/MainMenu.xaml", System.UriKind.RelativeOrAbsolute));
    }

But this code does not seem to work. The expected delay (3 seconds) is not there. Can anyone please help?

Best Regards


Solution

  • Two things you can try (and a last minute thought).

    1st, I've seen people complain the Thread.Sleep() isn't working, but in each case they've been doing it on the UI thread. I've used Thread.Sleep() successfully myself, but when doing so it's been in a background worker thread. I haven't investigated this further as yet, but you may like to check it out.

    A good walkthrough on using background worker here (posted by Pham Tien Sinh on msdn).

    Phạm Tiểu Giao - Threads in WP7

    2nd, you coud alternatively implement a timer. Two options for that here.

    DispatcherTimer Class (System.Windows.Threading)

    Timer Class (System.Threading)

    Oh, one other thing that comes to mind, since you're doing some startup navigation trickery... checkout Peter Torr's posts on the topic of places and redirecting navigation. You might find some useful gems there depending what you're trying to do. Good content to be aware of in any case.

    Introducing the concept of “Places” - Peter Torr's Blog

    Redirecting an initial navigation - Peter Torr's Blog