MvvmCross 8按钮绑定Command无效 点击无法跳转页面如何解决
问题排查与修复方案
1. SecondViewModel定义错误
你提供的SecondViewModel.cs代码中类名仍为FirstViewModel,属于复制粘贴失误,项目中不存在可被导航服务识别的SecondViewModel类型,是跳转无响应的核心原因。
修正后代码:
using MvvmCross.ViewModels; namespace Babel2.Core.ViewModels { public class SecondViewModel : MvxViewModel { // 无额外业务逻辑可保留空实现 } }
2. SecondView载体类型不匹配
MvvmCross默认的Navigate<目标ViewModel>方法会优先查找绑定了对应ViewModel的Activity作为跳转目标,但你当前的SecondView继承自MvxFragment,没有对应的Activity承载,无法直接跳转显示。
推荐使用更符合页面跳转需求的Activity方案进行修改,调整SecondView.cs代码如下:
using Android.App; using Android.OS; using MvvmCross.Platforms.Android.Views; namespace Babel2.Droid.Views { [Activity(Label = "第二页")] public class SecondView : MvxActivity<SecondViewModel> { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.SecondView); } } }
可选优化项
FirstViewModel中的GoCommand建议调整为初始化时一次性生成的只读属性,同时适配异步导航方法避免潜在异常:
using MvvmCross.Commands; using MvvmCross.Navigation; using MvvmCross.ViewModels; namespace Babel2.Core.ViewModels { public class FirstViewModel : MvxViewModel { private readonly IMvxNavigationService _navigationService; public IMvxCommand GoCommand { get; } public FirstViewModel(IMvxNavigationService navigationService) { _navigationService = navigationService; GoCommand = new MvxCommand(async () => await _navigationService.Navigate<SecondViewModel>()); } } }
内容的提问来源于stack exchange,提问作者user1239299
相关产品推荐
相关产品推荐

