【问题标题】:MvvmCross with Xamarin.Forms: Setup not calledMvvmCross 与 Xamarin.Forms:未调用安装程序
【发布时间】:2017-06-29 18:57:25
【问题描述】:

我正在尝试将我现有的 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,甚至没有调用构造函数。我对吗?代码有什么问题?

【问题讨论】:

    标签: c# xamarin xamarin.forms mvvmcross


    【解决方案1】:

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

    您必须从您的 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 :-)
    • 在 MvvmCross 5.0 中可能会支持没有启动画面的引导程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多