当我的应用程序启动时,我会显示带有图像的故事板,就像启动屏幕一样。然后我从 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 为空。有什么建议我做错了吗?