MVVMCross Android按钮绑定将其禁用

时间:2018-10-12 10:54:10

标签: android xamarin.android mvvmcross

我正在尝试使用MvvmCross将简单命令绑定到Android的按钮。 问题是,与我绑定命令相比,该按钮进入了禁用状态。

在绑定local:MvxBind="Click GotoScreen2Command"被删除的情况下,该按钮被启用并且可以单击。

为什么会这样,我该如何解决?

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> 

这是我的Android视图。

MainFragment.cs

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

还有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 个答案:

答案 0 :(得分:1)

您似乎需要将命令代码移至MainViewModel中。您的MainFragment使Resource.Layout.Main膨胀,并且您的MainFragment绑定到您的MainViewModel