gpt4 book ai didi

c# - MVVM 在 VievModel 中使用 ShowViewModel 进行交叉导航

转载 作者:行者123 更新时间:2023-12-03 10:33:39 26 4
gpt4 key购买 nike

我正在使用 MVVMCross 在 Xamarin 中制作应用程序。这是我的代码:

Android View (iOS 几乎一样):

var button = (Button)FindViewById(Resource.Id.button3);
var set = this.CreateBindingSet<MenuView, MenuViewModel>();
set.Bind(button).To(vm => vm.CommandNavigateToSecondPage);
set.Apply();

核心 View 模型:
public ICommand CommandNavigateToSecondPage
{
get
{
return new MvxCommand((() =>
{
ShowViewModel<SecondPageViewModel>();
}));
}
}

我想要一个默认的后退按钮,可以将我导航到上一页。我在核心中使用简单的功能进行了相同的导航,并且有后退按钮。像这样:
public void Navigate()
{
ShowViewModel<SecondPageViewModel>();
}

MVVM 是关于绑定(bind)的,这就是我想以这种方式制作它的原因。

最佳答案

在android中显示默认的主页/返回按钮

[Activity (Label = "MyView")]           
public class MyView : MvxActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.ThirdNativeView);

ActionBar.SetDisplayHomeAsUpEnabled(true);
}
/// handle back navigation here
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
//Execute viewModel command here
this.Finish();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
}

有关执行命令的更多信息 here

关于c# - MVVM 在 VievModel 中使用 ShowViewModel 进行交叉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com