Can I implement MVVM with Binding in Xamarin.Android?

LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

I'm trying to implement an Android client for my current Universal App application using Shared Project with running clients in Windows Phone 8.1 and Windows 8.1 (Store).

Every developer here is used to MVVM and it's applied to our current clients but it seems not to be possible to do with Xamarin.Android. Does anyone know if there's an API or any workaround for it?

Regards.

Answers

  • CheesebaronCheesebaron mod DKInsider, University mod

    There are several frameworks which provide bindings in one form or another. Amongst them are:
    MvvmCross, MvvmLight and ReactiveUI.

  • MihaMarkicMihaMarkic ✭✭✭✭ SI ✭✭✭✭

    Besides those, there is also Xamarin Forms and native binding support is coming soon.

  • JamesMontemagnoJamesMontemagno Xamurai USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    MVVM in general is just an architectural pattern, it is a framework that plugs in the data binding. You could easily subscribe/unsubscribe to propertychangednotifications and roll your own. Something light weight is MVVMLight though.

  • PrzemyslawRaciborskiPrzemyslawRaciborski PLMember

    Check MVVMCross - if you need more sophisticated help PM.

  • CheesebaronCheesebaron mod DKInsider, University mod

    If you want something super simple just to do databinding, it might not be worth doing it yourself as @JamesMontemagno says. Unless you have the time of course.

    Praeclarum on GitHub has this light weight binding engine called... Bind, which does very simple binding. Not in the markup, but in your controller/activity/fragment: https://github.com/praeclarum/Bind

  • LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

    I've tried using apis like MvvmCross but they wont fit my solution, because they expect to have viewmodels in a PCL and PCL does no support targeting Xamarin.Android, Xamarin.iOS, Windows Phone 8.1 (not SilverLight) and Windows 8.1 Store.

    My next step will be trying to implement a very dumb mvvm api myself as @JamesMontemagno said. I'll keep in touch for further questions for sure. Thanks for your advise.

    I'm going to take a look over that github implementation @Cheesebaron thanks!

  • MihaMarkicMihaMarkic ✭✭✭✭ SI ✭✭✭✭

    @LautaroRodriguezGarrido That's easy to solve, go with IoC/DI (inversion of control, dependency injection) approach. define the interface in PCL and implement it at platform level.

  • CheesebaronCheesebaron mod DKInsider, University mod

    PCL does no support targeting Xamarin.Android, Xamarin.iOS, Windows Phone 8.1 (not SilverLight) and Windows 8.1 Store.

    Uhhh... Yes it does! Profile 111 does exactly that!

    Imgur

  • ArturMalendowiczArturMalendowicz ✭✭ USMember ✭✭

    I've tried using apis like MvvmCross but they wont fit my solution, because they expect to have viewmodels in a PCL and PCL does no support targeting Xamarin.Android, Xamarin.iOS, Windows Phone 8.1 (not SilverLight) and Windows 8.1 Store.

    Well, PCL must be platform-independent. That's why ViewModels cannot contain any Xamarin.Android/Xamarin.iOS/WinPhone data structures.

    Regarding bindings in Xamarin.Android, I would recommend you to go with mvvmcross. It's really easy to implement it into project and then you just has to use in your axml binding like:

    <Label
     ...
    local:mvxbind="Text ViewModelTextValue" />
    

    and it works great!

  • LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

    @Cheesebaron any ideas of what's missing? Maybe something I have to install in my VS2013 to support this selection.

    @ArturMalendowicz my problem is the one shown in the image. If there's a way to solve it then MvvmCross can be used. If not, I can't share my viewmodels

  • CheesebaronCheesebaron mod DKInsider, University mod

    Uhm dunno, works fine for me on 3 different machines. Must be something up with your environment. Which update are you running on your VS2013 install?

  • LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

    I'm running update 4

  • rmaciasrmacias ✭✭✭✭✭ USBeta, University ✭✭✭✭✭

    Are you running on Windows 7 or Windows 8?

  • LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

    Windows 8.1
    Visual Studio 2013.4

  • LautaroRodriguezGarridoLautaroRodriguezGarrido USMember, University

    After some time searching the cause of my problem I've got to Dan Rigsby's blog and this post particulary: http://danrigby.com/2014/04/10/windowsphone81-pcl-xamarin-fix/

    It seems as my Profile 111 and 259 were not targeting all that they should and this little workaround did the trick. Now I'm able to go for MvvmCross. Thanks to all!

Sign In or Register to comment.