MvvmCross / MvvmCross-Forms

Support for Xamarin.Forms on MvvmCross: The .NET MVVM framework for cross-platform solutions, including Xamarin.iOS, Xamarin.Android, Windows and Mac.
http://mvvmcross.com
6 stars 2 forks source link

Allow to find Xamarin.Forms pages in different assembly #4

Closed stefandevo closed 9 years ago

stefandevo commented 9 years ago

This change allows that ViewModels and Pages are not in the same assembly. Original code was looking for Pages in the same assembly as the requested ViewModel.

The standard behavior has not changed from the Original code; however it is now possible to implement your own MvxFormsPageLoader instance with your own logic to "find" the page. You then have to register the IMvxFormsPageLoader into your IOC container.

Example. I have an assembly with the ViewModels (no reference to Xamarin.Forms); another assembly that contains the Pages (references Xamarin.Forms). In the Xamarin.Forms assembly I created a custom PageLoader.

    public class MyFormsPageLoader : MvxFormsPageLoader
    {
        protected override Type GetPageType(string pageName)
        {
            var types = GetType().GetTypeInfo().Assembly.CreatableTypes();
            var pageType = types.FirstOrDefault(t => t.Name == pageName);
            return pageType;
        }
    }

In order to get this page loader inside the IOC container I have to add following code in each platforms Setup.cs:

        protected override void InitializeViewLookup()
        {
            base.InitializeViewLookup();
            Mvx.RegisterSingleton<IMvxFormsPageLoader>(new MyFormsPageLoader());
        }

Of course, the above can be accomplished in the Initialization of the App.cs, but in my case the App.cs has no reference towards Xamarin.Forms (only viewmodels are there).

Comments welcome!

stefandevo commented 9 years ago

An extra addition could be to look for different pages based upon the fact you are running on a tablet. You can create Pages with extensions Tablet for example.

Cheesebaron commented 9 years ago

An idea could be to use Attributes to classify the Page whether it is Tablet or Phone idiom?

stefandevo commented 9 years ago

Would be nice if this PR could be merged...

martijn00 commented 9 years ago

@stefandevo i'll merge this now, can you open a new PR to have support for Tablet and phone?