【问题标题】:How is Setup class instantiated in MVVMCross in Xamarin?如何在 Xamarin 的 MVVMCross 中实例化 Setup 类?
【发布时间】:2018-01-06 21:30:35
【问题描述】:

我开始学习 MVVM 交叉,在 android 应用程序中,我有一个闪屏类:

 [Activity(MainLauncher = true,
        Label = "@string/app_name",
        Theme = "@style/Theme.Splash",
        NoHistory = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
        ScreenOrientation = ScreenOrientation.Portrait)]
    public class SplashScreen : MvxSplashScreenActivity
    {
        public SplashScreen() : base(Resource.Layout.SplashScreen)
        {

        }
    }

这是设置类:

public class Setup : MvxAndroidSetup
    {
        protected Setup(Context applicationContext) : base(applicationContext)
        {
        }

        protected override IMvxApplication CreateApp()
        {
            return null;
        }
    }

问题是调试器没有命中设置类的构造函数,而是在启动屏幕的构造函数之后得到“未处理的异常”

编辑

我已经在 PCL 项目中定义了 App 类:

public class App : MvxApplication
{
    public override void Initialize()
    {
        base.Initialize();

    }

还定义了 AppStart:

 public class AppStart : MvxNavigatingObject, IMvxAppStart
        {
            public async void Start(object hint = null)
            {
                //hardcoded login for this demo
                //var userService = Mvx.Resolve<IUserDataService>();
                //await userService.Login("gillcleeren", "123456");

                ShowViewModel<MainViewModel>();
            }
        }

这个项目背后的主要原因是为了了解 MVVM Cross 所需和执行的代码序列,所以我提供了最少的代码,直到它成功运行而没有运行时错误。

【问题讨论】:

    标签: c# android xamarin mvvm mvvmcross


    【解决方案1】:

    更新

    我再次更彻底地阅读了您的代码,现在我可以看到问题所在。您将Setup 类的构造函数定义为protected,这使其对激活不可见。

    在 Android 的 MvvmCross 上,魔法发生在 MvxAndroidSetupSingleton 类中(参见 source code here),它搜索您定义的 Setup 类型。 FindSetupType 方法首先查找您定义的 Setup 类,然后在 CreateSetup 方法中查找 Activator.CreateInstance 用于构建 Setup 实例。但是,使用的CreateInstance 方法变体仅搜索public 构造函数,这意味着它找不到受保护的构造函数。结果是它无法构建Setup 类并崩溃。

    原答案

    发生这种情况的原因是您没有 Core 库来定义 MvvmCross App 类并初始化其他所需的设置。我建议您从 simple tutorial 开始,或查看 official sample projects 以了解使 MvvmCross 在 Xamarin.Android 应用程序中工作所需的条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      相关资源
      最近更新 更多