简体   繁体   中英

How is Setup class instantiated in MVVMCross in Xamarin?

I'm starting learning MVVM cross, In the android app, I have a splash screen 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)
        {

        }
    }

and this is the Setup class:

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

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

the problem is that the debugger doesn't hit the constructor of the Setup Class, instead I get "An unhandled exception" after the constructor of the splash screen

在此处输入图片说明

EDIT

I've already defined the App class in the PCL project:

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

    }

also defined the 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>();
            }
        }

The main reason behind this project is to understand the sequence of code required and executed by MVVM Cross, so I provide the minimum code till it runs successfully without runtime errors.

Update

I have read your code again more thoroughly and I can see the issue now. You defined the constructor of the Setup class as protected , which makes it invisible for activation.

On MvvmCross for Android the magic happens inside MvxAndroidSetupSingleton class (see the source code here ) which searches for the Setup type you defined. The FindSetupType method looks for your defined Setup class first and then inside the CreateSetup method Activator.CreateInstance is used to build the Setup instance. The CreateInstance method variant used however searches only for public constructors, which means it doesn't find your protected one. The result is that it cannot build the Setup class and crashes.

Original answer

The reason this happens is that you have no Core libary that would define the MvvmCross App class and would initialize other required setup. I suggest you to start with a simple tutorial or to look into the official sample projects to see what is necessary to make MvvmCross work in a Xamarin.Android app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM