0

I am using Visual Studio Express 2012 for Windows Phone and building an app targeting Windows Phone 7.1.

I am trying to use NavigationService to navigate to a different page but I keep running into problems.

Here is my code:

private void GotoDetails(object _url)
    {
        var url = string.Format("/DetailsPage.xaml?url={0}", _url.ToString());
        NavigationService nav = new NavigationService();
        nav.Navigate(new Uri(url, UriKind.Relative));
    }

when I try to build this I get a "The type 'System.Windows.Navigation.NavigationService' has no constructors defined" on the NavigationService nav =.... line.

If I try to do just NavigationService.Navigate(new Uri(url, UriKind.Relative)); then I get a "An object reference is required for the non-static field, method, or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)'" error.

update: I am trying to use MVVM (for the first time). This code resides in my view model. I am not using any frameworks. Just trying to learn it from scratch.

I have searched the intertubes but cannot seem to find a solution.

Any help is much appreciated.

Kamal

Kamal
  • 383
  • 1
  • 6
  • 16

1 Answers1

2

are you inside a page? NavigationService is a property of the Page class: http://msdn.microsoft.com/en-us/library/system.windows.controls.page.navigationservice(v=vs.92).aspx

dotMorten
  • 1,953
  • 1
  • 14
  • 10
  • I am trying to use MVVM (for the first time). This code resides in my view model. – Kamal Nov 17 '12 at 18:55
  • yeah the navigation isn't that mvvm friendly. I consider it part of the view so I usually don't have that problem. But you could use this from the viewmodel (still not too pretty): var root = App.RootVisual as PhoneApplicationFrame; root.Navigate(new Uri("/somepage.xaml", UriKind.Relative)); – dotMorten Nov 18 '12 at 16:53
  • I also found this post to be helpful in creating a navigation service to use with MVVM: http://www.windowsphonegeek.com/articles/MVVM-in-real-life-Windows-Phone-applications-Part2 – Kamal Nov 18 '12 at 19:35