gpt4 book ai didi

c# - MvvmCross 与 Xamarin.Forms : Setup not called

转载 作者:行者123 更新时间:2023-11-30 16:45:24 24 4
gpt4 key购买 nike

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

最佳答案

引导在启动画面中完成。

您必须从 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

关于c# - MvvmCross 与 Xamarin.Forms : Setup not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42176125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com