How to reference UI-Component in the ViewModel in MVVMCross

508 Views Asked by At

I am using Xamarin, MVVMCross and visual studio for Android development. Is there any way in android layout in Xamarin to reference the UI component in the VM as shown below in the code?

what is the purpose of having a reference to the entire UI-Component.?

code

 <Button 
            x:Name="buttonItem" 
            Text="{Binding ButtonText}" 
            BackgroundColor="{x:Static resx:AppConstants.TransparentColor}"
            BorderColor="{x:Static resx:AppConstants.TransparentColor}"
            HorizontalOptions="Start"
            />
2

There are 2 best solutions below

0
JKL On

No you bind the properties of the button to properties in your view model. You don't bind your complete button to something in your view model.

0
Pavlos On

You should never reference UI components in your viewmodels. MVVM tries to maintain a certain separations of concerns. You should take a look at the databinding fundamentals for mvvmcross https://www.mvvmcross.com/documentation/fundamentals/data-binding.