Setup class 如何在 Xamarin 的 MVVMCross 中实例化?
How is Setup class instantiated in MVVMCross in Xamarin?
我开始学习 MVVM cross,在 android 应用程序中,我有启动画面 class:
[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)
{
}
}
这是设置 class:
public class Setup : MvxAndroidSetup
{
protected Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return null;
}
}
问题是调试器没有命中 Setup Class 的构造函数,而是在初始屏幕
的构造函数之后得到 "An unhandled exception"
编辑
我已经在 PCL 项目中定义了 App class:
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 所需和执行的代码序列,所以我提供了最少的代码,直到它成功运行而没有运行时错误。
更新
我已经更彻底地再次阅读了您的代码,现在我可以看到问题所在了。您将 Setup
class 的构造函数定义为 protected
,这使得它在激活时不可见。
在 Android 的 MvvmCross 上,魔术发生在 MvxAndroidSetupSingleton
class 中(参见 source code here),它搜索您定义的 Setup
类型。 FindSetupType
方法首先查找您定义的 Setup
class 然后在 CreateSetup
方法内部 Activator.CreateInstance
用于构建 Setup
实例。但是,使用的 CreateInstance
方法变体仅搜索 public
构造函数,这意味着它找不到您的受保护构造函数。结果是它无法构建 Setup
class 并崩溃。
原回答
发生这种情况的原因是您没有 Core
库来定义 MvvmCross
应用程序 class 并初始化其他所需的设置。我建议您从 simple tutorial or to look into the official sample projects 开始,看看让 MvvmCross 在 Xamarin.Android 应用程序中工作需要什么。
我开始学习 MVVM cross,在 android 应用程序中,我有启动画面 class:
[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)
{
}
}
这是设置 class:
public class Setup : MvxAndroidSetup
{
protected Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return null;
}
}
问题是调试器没有命中 Setup Class 的构造函数,而是在初始屏幕
的构造函数之后得到 "An unhandled exception"编辑
我已经在 PCL 项目中定义了 App class:
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 所需和执行的代码序列,所以我提供了最少的代码,直到它成功运行而没有运行时错误。
更新
我已经更彻底地再次阅读了您的代码,现在我可以看到问题所在了。您将 Setup
class 的构造函数定义为 protected
,这使得它在激活时不可见。
在 Android 的 MvvmCross 上,魔术发生在 MvxAndroidSetupSingleton
class 中(参见 source code here),它搜索您定义的 Setup
类型。 FindSetupType
方法首先查找您定义的 Setup
class 然后在 CreateSetup
方法内部 Activator.CreateInstance
用于构建 Setup
实例。但是,使用的 CreateInstance
方法变体仅搜索 public
构造函数,这意味着它找不到您的受保护构造函数。结果是它无法构建 Setup
class 并崩溃。
原回答
发生这种情况的原因是您没有 Core
库来定义 MvvmCross
应用程序 class 并初始化其他所需的设置。我建议您从 simple tutorial or to look into the official sample projects 开始,看看让 MvvmCross 在 Xamarin.Android 应用程序中工作需要什么。