首页 文章

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

提问于
浏览
3

我正在尝试将我现有的 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 is not called ,甚至不是构造函数 . 我对吗?代码有什么问题?

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

相关问题