Home > DeveloperSection > Forums > Button Click will not navigate to a new WPF page
Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    December-23-2013 3:34 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 2080  View(s)
Rate this:

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

Total Post:411

Points:2881
Posted on    December-23-2013 5:20 AM

Hi Brad,

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.

Don't want to miss updates? Please click the below button!

Follow MindStick