Oct
10
Written by:
New Age Solution
10/10/2009 9:53 AM
When first time learning Silverlight even simple as going from Silverlight page to page seems to be strange. We will look at very simple solution to the problem of navigatin from page to page.
Lots of times we want to be able to navigate from Silverlight page to page in code behind or in ViewModel. Say we have button and on Click we want to programmatically navigate to this page.
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/About", UriKind.RelativeOrAbsolute));
}
Notice that we are using NavigationService. But how do we know where to navigate?
In MainPage.xaml:
<navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}"
Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
In NavigationService.Navigate, we can provide any of the Uri's specified in UriMapping.
That's it!