简体   繁体   中英

MvvmCross with Xamarin.Forms: Setup not called

I am trying to put together my existing Xamarin.Forms application with MvvmCross.Forms . Unfortunately I am not able to go through the initialization.

MainActivity.cs

[Activity(MainLauncher = true, Label = "Main Activity")]
public class MainActivity : FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        Forms.Init(this, bundle);
        var app = new MvxFormsApp();
        LoadApplication(app);

        var presenter = (MvxFormsDroidPagePresenter) Mvx.Resolve<IMvxViewPresenter>(); // Exception
        presenter.MvxFormsApp = app;

        Mvx.Resolve<IMvxAppStart>().Start();
    }
}

Setup.cs

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

    protected override IMvxApplication CreateApp()
    {
        return new App();
    }

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        var presenter = new MvxFormsDroidPagePresenter();
        Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);
        return presenter;
    }
}

I guess the problem is that the Setup is not called at all, not even the constructor. Am I right? What is wrong with the code?

The bootstrapping is done in the splash screen.

You have to remove MainLauncher = true from your MainActivity and add a splash screen like:

[Activity(MainLauncher = true
    , Theme = "@style/Theme.Splash"
    , NoHistory = true
    , ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen
    : MvxSplashScreenActivity
{
    public SplashScreen()
        : base(Resource.Layout.SplashScreen)
    {
    }

    private bool _isInitializationComplete;
    public override void InitializationComplete()
    {
        if (!_isInitializationComplete)
        {
            _isInitializationComplete = true;
            StartActivity(typeof(MainActivity));
        }
    }

    protected override void OnCreate(Android.OS.Bundle bundle)
    {
        Forms.Init(this, bundle);
        Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) =>
        {
            if (!string.IsNullOrWhiteSpace(e.View.StyleId))
            {
                e.NativeView.ContentDescription = e.View.StyleId;
            }
        };

        base.OnCreate(bundle);
    }
}

If you need a working example, see our example app: https://github.com/xabre/xamarin-bluetooth-le/tree/master/Source/BLE.Client

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