Navigating to the Current Page on WP7

Sometimes you want to be able to “reset” the page you’re on. For example, the page is a form with a bunch of data that gets filled out. When the user clicks save, you prompt them and ask if they want to add another. If so, then you need to reset the page back to it’s original state.┬áThis sounds easy enough to do straight from code, but resetting everything isn’t always as easy as it sounds.

I’ve found that navigating to the current page works the best. Then the page runs as new, and everything gets setup properly without any extra code.

By default, you can’t navigate to the same page; it just doesn’t do anything. The trick here is to make the URI unique.

var uri = new Uri( string.Format( "{0}?unique={1}", "SamePage.xaml, Guid.NewGuid() ), UriKind.RelativeOrAbsolute );
RemoveBackEntry = true;
NavigationService.Navigate( uri );
view raw Example1.cs hosted with ❤ by GitHub

You may also have noticed the line ‘RemoveBackEntry = true;’. When navigating back to yourself, you don’t want the page to keep getting added to the back stack. You can see how to remove the back entry in my previous post Navigating with MVVM on WP7.

Leave a Reply