BUTTON CLICK WILL NOT NAVIGATE TO A NEW WPF PAGE

Brad Pitt

Total Post:61

Points:429
Posted by  Brad Pitt
WPF 
WPF
 2672  View(s)
Ratings:
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.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Button Click will not navigate to a new WPF page

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!