MvvmCross覆盖Android的viewmodel关联

MvvmCross覆盖Android的viewmodel关联,android,viewmodel,mvvmcross,Android,Viewmodel,Mvvmcross,我正在尝试覆盖默认的视图模型关联,但到目前为止运气不佳。我需要覆盖此默认行为,因为我的某些视图模型不遵循默认视图模型查找假定的名称约定。例如,我在命名空间Pidac.Core.ViewModels中有一些ViewModels,而视图是在MyApplication.Droid.Views中定义的 我尝试在Droid项目的Setup.cs中提供显式类型映射,如下所示: protected override void InitializeViewLookup() {

我正在尝试覆盖默认的视图模型关联,但到目前为止运气不佳。我需要覆盖此默认行为,因为我的某些视图模型不遵循默认视图模型查找假定的名称约定。例如,我在命名空间Pidac.Core.ViewModels中有一些ViewModels,而视图是在MyApplication.Droid.Views中定义的

我尝试在Droid项目的Setup.cs中提供显式类型映射,如下所示:

 protected override void InitializeViewLookup()
        {
            var viewModelViewLookup = new Dictionary<Type, Type>()
            {
                  { typeof (FirstViewModel), typeof(FirstView) },     
                  { typeof (FloatSettingViewModel), typeof(FloatSettingView) },
                  { typeof (SettingsViewModel), typeof(SettingsView) },
                  { typeof (SearchResultDialogViewModel), typeof(SearchResultDialogView) },
            };

            var container = Mvx.Resolve<IMvxViewsContainer>();
            container.AddAll(viewModelViewLookup);      
        }
[Activity(Label = "settings", Theme = "@android:style/Theme.NoTitleBar")]
    public class FloatSettingView : MvxActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.FloatSettingView);
        }

        public new FloatSettingViewModel ViewModel
        {
            get { return base.ViewModel as FloatSettingViewModel; }
            set { base.ViewModel = value; }
        }
    }
 [Activity(Label = "settings", Theme = "@android:style/Theme.NoTitleBar")]
[MvxViewFor(typeof(FloatSettingViewModel))]
        public class FloatSettingView : MvxActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.FloatSettingView);
            } 

        }
仅使用第二种方法,在Setup.cs中不提供视图模型到视图类型映射,当框架尝试加载FloatSettingView时,我会遇到一个异常。异常情况是
未能找到Pidac.Core.ViewModels.FloatSettingViewModel的viewmodel。

我尝试了使用MvxViewForAttribute装饰视图的第三个选项,如下所示:

 protected override void InitializeViewLookup()
        {
            var viewModelViewLookup = new Dictionary<Type, Type>()
            {
                  { typeof (FirstViewModel), typeof(FirstView) },     
                  { typeof (FloatSettingViewModel), typeof(FloatSettingView) },
                  { typeof (SettingsViewModel), typeof(SettingsView) },
                  { typeof (SearchResultDialogViewModel), typeof(SearchResultDialogView) },
            };

            var container = Mvx.Resolve<IMvxViewsContainer>();
            container.AddAll(viewModelViewLookup);      
        }
[Activity(Label = "settings", Theme = "@android:style/Theme.NoTitleBar")]
    public class FloatSettingView : MvxActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.FloatSettingView);
        }

        public new FloatSettingViewModel ViewModel
        {
            get { return base.ViewModel as FloatSettingViewModel; }
            set { base.ViewModel = value; }
        }
    }
 [Activity(Label = "settings", Theme = "@android:style/Theme.NoTitleBar")]
[MvxViewFor(typeof(FloatSettingViewModel))]
        public class FloatSettingView : MvxActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.FloatSettingView);
            } 

        }
没有运气。我显然忽略了这里的一些东西。在我深入了解消息来源之前,有人做过这件事吗


TIA.

重写时,应该调用
base.InitializeViewLookup()
,这样就可以通过启动屏幕

“找不到视图模型”表明问题在于视图模型查找-您的app.cs是否与视图模型位于同一程序集中?如果没有,请尝试在安装程序中重写GetVirwModelAssemblies。斯图尔特(离家在外用手机-所以没有合适的答案)斯图尔特,就是这样,谢谢。App.cs与某些ViewModels不在同一程序集中。在安装程序中重写GetViewModelAssembly是我所要做的全部工作。不需要上述任何一项。再次感谢。您能解释一下您是如何重写
GetViewModelAssemblys
的吗?@ChaseFlorell,GetViewModelAssemblys是在MvxSetup中定义的受保护的虚拟方法。在相应平台项目根目录中的Setup.cs中定义的Setup类中重写此设置。对于Android,Setup.cs源自MvxAndroidDialogSetup。