Search code examples
windows-8.1mvvm-lightwindows-phone-8.1portable-class-librarywin-universal-app

Universal app with PCL - navigate to proper View


I have question whether it is possible somehow use this project structure and within some ViewModel navigate to specific View from Core.Windows/Core.WindowsPhone.

1. PCL libraries

Core (PCL for universal apps)

  • ViewModel

Core.Windows (PCL) - reference to Core

  • View

Core.WindowsPhone (PCL) - reference to Core

  • View

I need to keep View in PCL because I use the same View for some other projects.

2. Universal app

Windows - reference to Core, Core.Windows

WindowsPhone - reference to Core, Core.WindowsPhone

Shared


Solution

  • Yes I think it is possible.

    I have a similar requirements in my solution (using same View for different apps)

    Basicly I suggest the following:

    1) Create a NavigationService abstract class and/or INavigationService interface in Core (PCL for universal apps with ViewModels).

    2) Create WindowsNavigationService and WindowsPhoneNavigationService classes in Core.Windows and Core.WindowsPhone which derive from NavigationService in 1)

    This classes can do the following:

    1) Hold an instance of

       Dictionary<ViewModelBase,Page> mapDictionary
    

    ViewModelBase is a base class for your viewmodel (I suppose you have one).

    2) Register the data in mapDictionary: e.g. add View and corresponding ViewModel to it

    3) Perform navigation buy holding the instance of Frame: e.g. NavigateTo, GoBack and so on

    4) Bind the ViewModel to View.

    After that you can Hold a NavigationService in your ViewModels and perform navigation between them.

    If you are interested in such approach I suggest to waste some time and take a look at https://github.com/MvvmCross/MvvmCross - this can give you ideas about how you can build your architecture.

    Also interesting post about PCL (with sample code demonstrating described approach): http://msdn.microsoft.com/en-us/magazine/hh852593.aspx

    Source code for the article: http://download.microsoft.com/download/6/2/D/62D36C65-4C36-429A-8711-F657D34AB4BC/Code_KeanPCL0312.zip