MvvmCross与Xamarin.Forms:安装程序未调用

时间:2017-02-11 12:57:29

标签: c# xamarin xamarin.forms mvvmcross

我正在尝试将我现有的 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;
    }
}

我想问题是安装程序根本没有被调用,甚至不是构造函数。我对吗?代码有什么问题?

1 个答案:

答案 0 :(得分:2)

引导在启动画面中完成。

您必须从MainLauncher = true中删除MainActivity,然后添加如下屏幕:

[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