0

当我的应用程序启动时,我会显示带有图像的故事板,就像启动屏幕一样。然后我从 AppDelegate 类中的 FinishedLaunching() 方法调用 LoadApplication(new App())。在我的 App.cs 中,我需要访问 UIApplication.SharedApplication.KeyWindow.RootViewController 以显示进度条。但是 Keywindow 是空的,因此会发生崩溃。

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
DependencyService.Register<ILoadingService,LoadingService>();
global::Xamarin.Forms.Forms.Init();
Xamarin.FormsGoogleMaps.Init("AIzaSyCqt-tfGrKhauCgC2Y5UkPreXfMZisPOH8");

LoadApplication(new App());
UIWindow.Appearance.TintColor = new UIColor(red: 0.55f, green: 0.76f, blue: 0.29f, alpha: 1.0f);
return base.FinishedLaunching(app, options);
}

App.cs 文件:

public App(string ticketNumberNotifParam = null, int ticketID = 0)
{
DependencyService.Get<ILoadingService>().Show("Updating user token...");
LoadHomepage();
}

加载服务();

public void Show(string title, string message = "Loading")
{
UIViewController controller = 
UIApplication.SharedApplication.KeyWindow.RootViewController;
        hud = new MTMBProgressHUD(controller.View);
        controller.View.AddSubview(hud);
}

这里 Keywindow 为空。有什么建议我做错了吗?

4

1 回答 1

1

原因:

此时keyWindow尚未实例化。

解决方案:

您可以将您的show功能放在OnStart.

 protected override void OnStart()
    {
        // Handle when your app starts

        DependencyService.Get<ILoadingService>().Show("Updating user token...");

    }

参考:应用程序生命周期

于 2019-01-24T05:23:11.230 回答