2

我正在尝试覆盖默认视图模型关联,但到目前为止还没有运气。我需要覆盖此默认行为,因为我的某些 ViewModel 不遵循默认视图模型查找所假定的名称约定。例如,我在命名空间 Pidac.Core.ViewModels 中有一些 ViewModel,而 View 是在 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);      
        }

有了这个,我的应用程序不再经过 MvvmCross 徽标屏幕。

或者,我尝试在 FloatSettingView.cs 中提供具体的视图模型类型,如下所示:

[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; }
        }
    }

仅使用第二种方法,而不提供视图模型来查看 Setup.cs 中的类型映射,当框架尝试加载 FloatSettingView 时,我得到一个异常。例外是 Failed to find viewmodel for Pidac.Core.ViewModels.FloatSettingViewModel.

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

 [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。

4

1 回答 1

1

您应该base.InitializeViewLookup()在覆盖时调用,这样您就可以越过启动画面。

于 2018-07-11T08:18:03.257 回答