Xamarin Forms error - 'You MUST call Xamarin.Forms.Init(); prior to using it' - TestFlight (iOS) only

泄露秘密 提交于 2020-02-03 08:42:11

问题


I've seen several other posts on SO and elsewhere about this error (System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it. at Xamarin.Forms.Device.get_PlatformServices), but none of them shed any light on my particular situation.

I've created a Xamarin.Forms application that works great on Android (emulators and device, including when distributed through Google Play), and works fine both on the iPhone simulator and when provisioned to the iPhone. And when I say 'works fine', I mean 'is able to get to the MainPage without error.'

However, when I use Ad-Hoc deployment and upload it to Apple so it can be distributed with TestFlight, the TestFlight-downloaded version shows the splash screen as expected, and then crashes without ever showing the MainPage. In looking at the device logs, the above error appears, and in doing some Internet research, it's become apparent that my AppDelegate needs to have global::Xamarin.Forms.Forms.Init(); as the first line in the FinishedLaunching() method.

So I went to take a look and put that line in that method in that file, and lo and behold, it's already there. This is not unexpected, given the fact that the app works on the sim, and on the iPhone when deployed directly from Visual Studio.

So my question is 'Why does Ad-Hoc deployment fail when all other deployment types do not, and what can I do to overcome this problem and get the app to actually distribute through TestFlight in a runnable fashion?'


回答1:


I was able to resolve the issue by putting the command (global::Xamarin.Forms.Forms.Init();) first thing in every constructor in every class in the iOS platform-specific services. It's possible that only one was really necessary, but it doesn't seem to have caused any issue to over-deploy that particular command, and in the case that more than one of the services is called before the AppDelegate's FinishedLaunching() method is called, that would seem to be necessary in the first one called, which could potentially vary by program flow, depending on environment and other variables that may be checked.




回答2:


In .iOS project make sure the line "global::Xamarin.Forms.Forms.Init();" is the first line in the event "public override bool FinishedLaunching(UIApplication app, NSDictionary options)" in "AppDelegate.cs" file.

In .UWP VB project make sure the line "Xamarin.Forms.Forms.Init(e)" is after the line "AddHandler rootFrame.NavigationFailed, AddressOf OnNavigationFailed" in "Protected Overrides Sub OnLaunched(e As Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)" event in "App.Xaml.vb" file.

In .Android project "global::Xamarin.Forms.Forms.Init(this, savedInstanceState);" is after the line "base.OnCreate(savedInstanceState);" in "protected override void OnCreate(Bundle savedInstanceState)" event.

Hope this helps.



来源:https://stackoverflow.com/questions/42236071/xamarin-forms-error-you-must-call-xamarin-forms-init-prior-to-using-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!