Frage

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.

I tried searching around and this problem has been brought up many times, but no one seems to have a decent explanation or at least one that I have been able to successfully apply.

Could someone point me in the right direction?

War es hilfreich?

Lösung

I 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.

Checkout this article. http://paulstovell.com/blog/wpf-navigation

Hope it helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top