0
votes

I'm trying to bind simple command to Android's button with MvvmCross. The problem is, than I bind command, the button goes to disabled state.

With deleted binding local:MvxBind="Click GotoScreen2Command" the button is enabled and clickable.

Why this is happening and how can I fix it?

MainContainerViewModel.cs:

public class MainContainerViewModel : BaseViewModel
{
    readonly IMvxNavigationService _navigationService;

    public IMvxAsyncCommand GotoScreen2Command { get; set; }

    public MainContainerViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;

        GotoScreen2Command = new MvxAsyncCommand(NavigateToScreen2Async);
    }

    private Task NavigateToScreen2Async()
    {
        return _navigationService.Navigate<Screen2ViewModel>();
    }
}

fragment_main.axml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_goToScreen2"
        android:text="Go to Screen2"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        local:MvxBind="Click GotoScreen2Command"/>

</android.support.constraint.ConstraintLayout> 

And here are my Android views.

MainFragment.cs:

[MvxFragmentPresentation(typeof(MainContainerViewModel), Resource.Id.content_frame)]
public class MainFragment : BaseFragment<MainViewModel>
{
    protected override int FragmentLayoutId => Resource.Layout.fragment_main;
}

And MainContainerActivity.cs:

[Activity(
        Theme = "@style/AppTheme",
        WindowSoftInputMode = SoftInput.AdjustResize | SoftInput.StateHidden)]
    public class MainContainerActivity : BaseActivity<MainContainerViewModel>, IDrawerActivity
    {
        protected override int ActivityLayoutId => Resource.Layout.activity_main_container;                         
    }
1
Is MainContainerViewModel definitely the ViewModel for the View that inflates fragment_main.axml? I have had this issue myself but only when the command that a button is bound to does not exist on the ViewModel. If the command cannot be found (such as a typo) then the button is disabled. - wainy
@wainy I've added the code for activities. As you can see MainContainerActivity uses MainContainerViewModel, but inflates fragment_main. I'm not sure I understand how my main fragment layout and activity are connected to main container activity and layout. Could you please explain it to me? - Alex
It looks like you need to move your command code into your MainViewModel. Your MainFragment inflates Resource.Layout.Main, and your MainFragment is bound to your MainViewModel - wainy
@wainy You're right, that was the issue. Post your comment as an answer, so I can mark it as answered question. Thank you. - Alex

1 Answers

1
votes

It looks like you need to move your command code into your MainViewModel. Your MainFragment inflates Resource.Layout.Main, and your MainFragment is bound to your MainViewModel.