c# - 如何将 MvxCommand 附加到 BottomBar?

标签 c# xamarin mvvm xamarin.android mvvmcross

我正在使用 this库添加 BottomNavigationBar到我的项目。我也在为我的这个项目的框架使用 MvvmCross。我不知道如何绑定(bind) MvxCommand给我的BottomBar尽管。有谁知道如何做到这一点?

这大致是我的MvxCommandViewModel好像:

public ICommand OnTabSelectedCommand 
{
    get { return New MvxCommand(() => OnTabSelected()); }
}

我的 BottomBar创建看起来像这样:
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        var recycler = FindViewById<MvxRecyclerView>(Resource.Id.menuList);
        var layoutManager = new LinearLayoutManager(this);
        recycler.SetLayoutManager(layoutManager);
        recycler.NestedScrollingEnabled = false;


        var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbarFinder);
        SetSupportActionBar(toolbar);

        _bottomBar = BottomBar.AttachShy((CoordinatorLayout)FindViewById(Resource.Id.ListCoordinator),
                         FindViewById(Resource.Id.menuList), savedInstanceState);

        _bottomBar.SetItems(new[]
              { new BottomBarTab(Resource.Drawable.ic_recents, "Recents"),
                new BottomBarTab(Resource.Drawable.ic_favorites, "Favorites"),
                new BottomBarTab(Resource.Drawable.ic_nearby, "Nearby") }
            );

        _bottomBar.SetOnMenuTabClickListener(this);

        _bottomBar.SetActiveTabColor(Color.Red);

        _bottomBar.MapColorForTab(0, "#7B1FA2");
        _bottomBar.MapColorForTab(1, "#FF5252");
        _bottomBar.MapColorForTab(2, "#FF9800");
    }

    public void OnMenuTabSelected(int menuItemId)
    {
        // Do something
    }

    public void OnMenuTabReSelected(int menuItemId)
    {
        // Do Something
    }

最佳答案

如果您像这样声明您的事件:

public class MyView : MvxActivity<MyViewModel>

您将能够访问“MyViewModel”类型的属性“ViewModel”。

然后你只需要调用你的命令:
ViewModel.OnTabSelectedCommand.Execute();

此调用应包含在您的 switch 命令中:
public void OnMenuTabSelected(int menuItemId)
{
    // Do something
}

关于c# - 如何将 MvxCommand 附加到 BottomBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45019667/

相关文章:

c# - 如何抑制用户对 ListBox 控件的操作

c# - .NET/C# 与 Python 互操作

android - Mvvmcross:启动包含 ListView 的 Android 对话框

android - 重用 ViewModels - 将使用它的每个屏幕的状态

c# - 将多个 Caliburn.Micro View 模型绑定(bind)到同一 View 的不同实例

c# - 在设计模式下使用 visual studio 2012 中的报表查看器时出错

c# - 字符串如何在堆中分配内存?

ios - Xamarin iOS中使用NSSuperscriptAttributeName的NSAttributedString

c# - FCM - 重新调试应用程序后发送消息时出现 Android Xamarin NotRegistered 错误

c# - WPF:MVVM - 如果命令为空则禁用按钮