Unable to set ICommand properties in my view model

I am developing a simple Xamarin.Forms app using a MVVM approach with VS 2017. Everything works as expected in UWP but my Android project does not execute the lines of code that get/set ICommand properties at all. I can set a breakpoint on the line that sets the property value, as well as the line before and the line after. I break on both lines before and after, but not on the line that sets the property.

Here is my view model code:

public LoginPageViewModel(INavigation navigation) : base() 
{
        Navigation = navigation;
        LoginButtonClicked = new Command(Login, () => true);
        PasswordHintButtonClicked = new Command(PasswordHint, () => true);
}

And my XAML.cs:

btnLogin.SetBinding(Button.CommandProperty,  nameof(vm.LoginButtonClicked), BindingMode.OneWay);
btnPasswordHint.SetBinding(Button.CommandProperty, nameof(vm.PasswordHintButtonClicked), BindingMode.OneWay);

As I said, I can break on setting the Navigation property but not the command properties. If I move Navigation = navigation to the last line of the constructor I still break on it.

I've also tried:

private ICommand _loginButtonClicked; public ICommand LoginButtonClicked { get { return _loginButtonClicked ?? (_loginButtonClicked = new Command(Login, () => true)); } }

This should be pretty straightforward. Maybe I made a mistake configuring the Android emulator? I've been beating my head against this for a few days now, if anyone has seen something like this before I'd appreciate a nudge in the right direction.

Thanks!

Best Answer

  • kwinsor5kwinsor5 ✭✭
    Accepted Answer

    If anyone happens to come across this same problem, it was caused by missing Preserve attributes. I had added the attribute to all external assemblies but it's also required on my view models. For some reason the linker wasn't pulling in the entire class even though it was referenced in the .xaml.cs page.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You should post in Xamarin Forms forum

  • kwinsor5kwinsor5 Member ✭✭

    I can do that, this is pretty Android-specific though.

  • kwinsor5kwinsor5 Member ✭✭
    Accepted Answer

    If anyone happens to come across this same problem, it was caused by missing Preserve attributes. I had added the attribute to all external assemblies but it's also required on my view models. For some reason the linker wasn't pulling in the entire class even though it was referenced in the .xaml.cs page.

Sign In or Register to comment.