MvvmCross 与 Xamarin.Forms:未调用安装设置

4

我正在尝试将我的现有的 Xamarin.Forms 应用程序与 MvvmCross.Forms 结合起来。不幸的是,我无法完成初始化过程。

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

我猜问题在于Setup根本没有被调用,甚至连构造函数也没有被调用。我是对的吗?这段代码有什么问题吗?

1个回答

2

引导程序是在启动画面中完成的。

您需要从MainActivity中删除MainLauncher = true并添加一个启动画面,例如:

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

如果您需要一个工作示例,请查看我们的示例应用程序: https://github.com/xabre/xamarin-bluetooth-le/tree/master/Source/BLE.Client

为了在开始时保持简单,我避免了启动画面 :-) 这个项目也帮助我更进一步。Danke schön :-) - core
在MvvmCross 5.0中,可能会支持无启动画面的引导程序。 - Sven-Michael Stübe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接