Alex
Alex

Reputation: 350

MVVMCross Android button binding disables it

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;                         
    }

Upvotes: 0

Views: 470

Answers (1)

wainy
wainy

Reputation: 435

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.

Upvotes: 1

Related Questions