C# 如何将MvxCommand附加到底部栏?
我正在使用库向我的项目添加C# 如何将MvxCommand附加到底部栏?,c#,xamarin,mvvm,xamarin.android,mvvmcross,C#,Xamarin,Mvvm,Xamarin.android,Mvvmcross,我正在使用库向我的项目添加BottomNavigationBar。我还在这个项目的框架中使用MvvmCross。但是,我不知道如何将MvxCommand绑定到我的BottomBar。有人知道怎么做吗 下面是我的MvxCommand在ViewModel中的大致外观: public ICommand OnTabSelectedCommand { get { return New MvxCommand(() => OnTabSelected()); } } 我的BottomBar创建
BottomNavigationBar
。我还在这个项目的框架中使用MvvmCross。但是,我不知道如何将MvxCommand
绑定到我的BottomBar
。有人知道怎么做吗
下面是我的MvxCommand
在ViewModel
中的大致外观:
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>
protectedoverride void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var recycler=findviewbyd(Resource.Id.menuList);
var layoutManager=新的LinearLayoutManager(此);
回收器。SetLayoutManager(layoutManager);
recycler.NestedScrollingEnabled=false;
var toolbar=findviewbyd(Resource.Id.toolbarFinder);
设置支持操作栏(工具栏);
_bottomBar=bottomBar.AttachShy((CoordinatorLayout)FindViewById(Resource.Id.ListCoordinator),
FindViewById(Resource.Id.menuList),savedInstanceState);
_底部栏。设置项(新[]
{new BottomBarTab(Resource.Drawable.ic_recents,“recents”),
新的BottomBarTab(Resource.Drawable.ic_收藏夹,“收藏夹”),
新的BottomBarTab(Resource.Drawable.ic_nearly,“nearest”)}
);
_bottomBar.SetOnMenuTabClickListener(这个);
_bottomBar.SetActiveTabColor(Color.Red);
_底部栏。MapColorForTab(0,#7B1FA2”);
_底部栏。MapColorForTab(1,“#FF5252”);
_底部条形图。MapColorForTab(2,#FF9800”);
}
选定菜单上的公共无效(int menuItemId)
{
//做点什么
}
已重新选择菜单上的公共无效(int MENUTEMID)
{
//做点什么
}
如果您这样声明您的活动:
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>
此调用应包含在以下命令中的switch命令中:
public void OnMenuTabSelected(int menuItemId)
{
// Do something
}
在视图的“onmenutableselected”方法中添加“if(menuItemId==xxx)myViewModel.OnTabSelectedCommand.Execute()”。@hugo它不想工作。我正在调用
ListHomeViewModel.OnTabSelectedCommand.Execute()代码>,但这会导致错误。我是否需要将命令设置为static(静态)
?否,如果使用“公共类myview:MvxActivity”这样的声明,您将拥有一个名为“ViewModel”的属性,该属性的类型为“MyViewModel”。然后需要调用“ViewModel.OnTabSelectedCommand.Execute()”。抱歉使用了错误的viewmodel…@hugo它现在可以工作了!非常感谢。如果你想写一个答案,我会把它标对。