4

I developed an app based on Xamarin for iOS & Andsroid using mvvmcross. 2 weeks ago Xamarin announced Xamarin.Forms. Did someone succeeded porting Xamarin with mvvmcross to Camarin.Forms with mvvmcross? I want to reuse most of my code, I'm using: Messenger, Location, SQLite, etc.

It would be great if someone could provide sample .sln with some Xamarin.Forms & mvvmcross integration. I think that mvvmcross's devs are working on publishing another video (#43?) about this issue but I'm not sure about it.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Gadi Paz
  • 43
  • 1
  • 5
  • 3
    I'm not working on any video #43 at present. Xamarin said during their seminar that there were some X.F + MvvmCross samples coming soon - that was a couple of weeks ago, so they should be along any day now? – Stuart Jun 12 '14 at 06:31
  • 1
    Thanks Stuart, I'll ask them (Xamarin) and will share the information here. Hope they're gonna make the integration easy to use 'cause most of my app uses mvvmcross – Gadi Paz Jun 12 '14 at 09:20

2 Answers2

1

There is a Sample from Cheesebaron on github: https://github.com/Cheesebaron/Xam.Forms.Mvx

Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103
1

Cheesebaron provided a very good example of combining those worlds together.

Adrian Sudbury, the creator of the Ninja Coder VS Extension, is planning to release a Xam.Forms + MvvmCross option in the extension soon.

I missed one important thing in Cheesebaron's solution. You have no possibility to inject dependencies into the ContentPages (you might argue if this makes sense). One use case here would be the MvvmCross Messenger PlugIn. An example could be if you want to send a message to your View to display an alert dialog when the login fails etc.

Cheesebaron's MvxPresenterHelper class is gluing the ViewModels together with the ContentPages. But the class is not using the provided IoC container from MvvmCross (Mvx) and is also doing the reflection process by itself, not providing the ability to inject dependencies into the Views.

I rewrote the Helper class to use the MvvmCross IoC container itself. You still have the ability to use other containers without changing your helper code. Reflection will be minimized because you don't need to scan the whole assembly, just the namespace where the Pages are stored.

MvxPresenterHelper.cs

public static ContentPage CreatePage(MvxViewModelRequest request)
    {
        var viewModelName = request.ViewModelType.Name;
        var pageName = viewModelName.Replace("ViewModel", "Page");
        Type pageType = Type.GetType(App.PageNamespace + "." + pageName);

        if (pageType == null)
        {
            Mvx.Trace("Page not found for {0}", pageName);
            return null;
        }

        var page = Mvx.Resolve(pageType) as ContentPage;

        if (page == null)
        {
            Mvx.Error("Failed to create ContentPage {0}", pageName);
        }
        return page;
    }

Notice the App.PageNamespace property. It stores the specific namespace for the ContentPages.

You need to register your pages in the App.cs

public class App : MvxApplication
{
    public static string PageNamespace { get; private set; }

    public App()
    {
        PageNamespace = this.GetType().Namespace + ".Pages";
    }
    public override void Initialize()
    {
        // Mvx default registration here

        // Register all ContentPages
        this.CreatableTypes()
            .EndingWith("Page")
            .InNamespace(PageNamespace)
            .AsTypes()
            .RegisterAsDynamic();

        this.RegisterAppStart<MainViewModel>();
    }
}
saxos
  • 2,467
  • 1
  • 20
  • 21