I cannot figure out why my button click is not navigating to a new page. I have tried writing this code several ways, but none of them work. EnlargedScreenCap is a WPF page and is in the same directory as the window in which I want to load it.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
nav.Navigate(esc);
}
Written like this I get a null reference exception because NavigationService is not getting initialized.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("//EnlargedScreenCap.xaml"), UriKind.RelativeOrAbsolute);
}
When written like the above code, I get an invalid URI error, although nav is still null.
Pravesh Singh
23-Dec-2013I do not think you should be creating the NavigationService the way you are doing. Also if you want to enable navigation all this should be done in a Frame
<Frame x:Name="_mainFrame" />
Then you could do something like
_mainFrame.NavigationService.Navigate(new Uri("EnlargedScreenCap.xaml", UriKind.Relative));
Without the double slashes in front.