简体   繁体   中英

Portable class library: navigate to xaml page

I have a portable class library with 3 projects.

  • Project for common classes for Windows Phone and Windows tablet
  • Project for Windows Phone
  • Project for Windows tablet

In my common project i have a folder model and a folder view model. In my Windows Phone project i have a folder view.

Is it possible to navigate to a xaml page in the view folder of the Windows Phone project from a class in de viewmodel folder in the common project?

NavigationService.Navigate(); doesn't work here, and i can't find any nuget package to include this.

So my question is:

  • Is there another way to navigate to the xaml page?
  • Or is there a nuget package to add the NavigationService in the common project?

The NavigationService class is not available in PCL. You can confirm this by viewing the documentation on MSDN and clicking the 'Other Versions' dropdown.

You should rather use events. Say, your viewmodel class may have an event called NavigationRequested , which you raise instead of calling NavigationService.Navigate() , and your view subscribes to that event with something like this:

ViewModel.NavigationRequested += (s, e) => NavigationService.Navigate(GetUrlFor(e.PageId));

我已经将http://azerdark.wordpress.com/2010/04/23/multi-page-application-in-wpf/用于几个具有多页的项目,它可能对您也很有用,希望对您有所帮助。

Here is a sample that pretty much shows exactly how to do this: Sharing Code: Adding NavigationService

Here are some sources of more general information about using Portable Class Libraries:

Within MvvmCross we do this type of navigation by navigating to the ViewModel rather than to the View.

Working this way, the framework intercepts the ViewModel navigation and interprets it in a platform-specific way - eg:

  • on WindowsPhone it uses a Xaml url
  • on WindowsStore it uses the navigation service
  • on Android it uses Intents

For your own framework, you should be able to produce a similar abstraction - just have the ViewModel initiate a navigation to something abstract, and then have platform-specific code take care of the details in each case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM