简体   繁体   中英

Automatically navigate to a new page after a time in UWP

I'm trying to transition/navigate to a new page after a time (let's say 3 seconds), but nothing worked. I've tried this.Frame.Navigate(typeof(NewPage)); and it gives me an error " Object reference not set to an instance of an object. ".

I've also tried

Thread.Sleep(3000); 
NewPage pg2 = new NewPage();
Window.Current.Content = pg2;

but it navigates immediately without waiting the 3 seconds.

The same problem I have also in am Xamarin app.

it gives me an error "Object reference not set to an instance of an object.

Please avoid use Thread.Sleep(3000) , it will block UI thread and make Frame as null. And as Silent Programmer mentioned, you could use DispatcherTimer to approach.

var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(3) };
timer.Start();
timer.Tick += (s, e) =>
{
    Frame.Navigate(typeof(NewPage));
    timer.Stop();
};

you can try using DispatcherTimer, Dispatcher timer is having tick event, set timer from interval to one second.

write if in timer_tick event and when tickCount hits 3, navigate to new page

Note: you may required to use coreDispatcher [If exception occures]

hope it help, if not work please comment down

Thank you for your responses. In the mean time I've soled my problem and the code I'm using is the following:

await Task.Delay(3000);

Frame navigationFrame = Window.Current.Content as Frame;
navigationFrame.Navigate(typeof(PageToNavigateTo));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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