CommunityToolkit / MVVM-Samples

Sample repo for MVVM package

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Xamarin Android Native

rvlietstra opened this issue · comments

Hi!
I'm evaluating MVVM frameworks for use in a xamarin android application. MVVMLight says they recommend your implementation, but it doesn't seem to directly support xamarin android native. Do you recommend still using MVVMLight in my case ?
Thanks!

Hi! It's not that the MVVM Toolkit doesn't support Xamarin Android Native, it's just that it was specifically designed to be platform agnostic and working everywhere, so we just listed some common frameworks as example, but left others out, like this one. The MVVM Toolkit should work just fine on Xamarin Android Native, as it is just a .NET Standard / .NET library that provides MVVM functionality with all the usual interfaces available in the BCL (eg. INotifyPropertyChanged). As long as the framework you're using supports MVVM in general (which Xamarin Android Native most definitely does), it should work just fine 😄

Let us know how it goes if you try this out!

FYI @michael-hawker I'm thinking we might maybe call this aspect of the library out more clearly in the docs somehow? I know it's mentioned in the intro, but this is not the first time I'm seeing this question being asked, so was wondering whether there was anything we might improve on this front to make it easier for folks to understand where the library can be used? 🙂

Hi!
Thanks I've now tried MVVMLight. I was under the impression that it somehow uses EditText.AfterTextChanged for example to get property changes, but it seems to work directly on the Text property. I assume EditText.Text implements INotifyPropertyChanged even tough I cannot find it in their source, which is why I assumed there has to be some android specific code.

I see https://docs.microsoft.com/en-us/windows/communitytoolkit/mvvm/migratingfrommvvmlight states that Binding/BindingMode is not implemented in the Community Toolkit.

With MVVMLight I simply did

var binding = SetBinding(()=> viewModel.Name, () => editText.Text, BindingMode.TwoWay);

How would I do the binding in the Community Toolkit ?
Thanks!

"I assume EditText.Text implements INotifyPropertyChanged"

It doesn't, the Text property is backed by a DependencyProperty, so the XAML compiler or runtime (depending on whatever framework you're using) will still know how to handle it properly and interact with the viewmodel you're binding to/from.

"Binding/BindingMode is not implemented in the Community Toolkit."

That is technically accurate but I think it's a bit confusing. What the docs state is not that you can't use binding or any of those binding mode when using the MVVM Toolkit, just that the specific Binding and BindingMode types from MVVMLight haven't been ported. But that is not necessary, because each UI framework already supports setting up bindings directly from XAML. You should avoid setting up bindings in code behind, and if you really need to do that (but seriously, don't), you can still do that using the various platform-specific APIs that are available on the framework you're using.

As in, to bind to Text you should just do something like this in XAML, as usual:

<TextBlock Text="{Binding MyTextProperty, Mode=OneWay}"/>

Does that help? 🙂

Hi.
Ah ok, I'll have to investigate how they do that with DependencyProperties (I cannot find references to them in the xamarin.android source either)

Unfortunately Xamarin.Android's .AXML seems not to support binding (unless I'm missing something). I'll have to find a way to do the binding explicitly like in MVVMLight.

Thanks!