当我的应用程序启动时,我会显示Storyboad,里面有一个像溅屏一样的图像。然后从LoadApplication类中的FinishedLaunching()方法调用AppDelegate (new())。在我的UIApplication.SharedApplication.KeyWindow.RootViewController中,我需要访问App.cs来显示进度条。但是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();
}LoadingService();
public void Show(string title, string message = "Loading")
{
UIViewController controller =
UIApplication.SharedApplication.KeyWindow.RootViewController;
hud = new MTMBProgressHUD(controller.View);
controller.View.AddSubview(hud);
}这里的Keywindow是空的。我做错了什么有什么建议吗?
发布于 2019-01-24 05:23:11
事业:
此时还没有实例化keyWindow。
解决方案:
您可以将show函数置于OnStart下。
protected override void OnStart()
{
// Handle when your app starts
DependencyService.Get<ILoadingService>().Show("Updating user token...");
}参考:应用生命周期
https://stackoverflow.com/questions/54333021
复制相似问题