带有 Prism 的表单:应用程序 windows 应具有根 VC
Forms with Prism: Application windows are expected to have a root VC
我刚刚在我的新应用程序中实现了 Prism 库,并且我正在使用 NavigationService。它在 Android 上运行良好,但是当我尝试在网络 Mac 上调试我的 Xamarin Forms iOS 应用程序时,我的 Main 方法抛出此异常:
Unhandled Exception:
Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch
Native stack trace:
0 CoreFoundation 0x00000001021f2b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010cdcf141 objc_exception_throw + 48
2 CoreFoundation 0x00000001021f6cf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x0000000102da93b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x0000000105ba0be6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 3343
5 UIKit 0x0000000105b9d793 -[UIApplication workspaceDidEndTransaction:] + 182
6 FrontBoardServices 0x000000010fae35f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
7 FrontBoardServices 0x000000010fae346d -[FBSSerialQueue _performNext] + 186
8 FrontBoardServices 0x000000010fae37f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
9 CoreFoundation 0x0000000102198c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
10 CoreFoundation 0x000000010217e0cf __CFRunLoopDoSources0 + 527
11 CoreFoundation 0x000000010217d5ff __CFRunLoopRun + 911
12 CoreFoundation 0x000000010217d016 CFRunLoopRunSpecific + 406
13 UIKit 0x0000000105b9c02f -[UIApplication _run] + 468
14 UIKit 0x0000000105ba20d4 UIApplicationMain + 159
15 ??? 0x0000000124f647fc 0x0 +
我的主要方法是这样的:
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
AppDelegate:
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
this.LoadApplication(new Core.CitizenApp());
return base.FinishedLaunching(app, options);
}
}
表单应用程序:
protected override void OnInitialized()
{
this.InitializeComponent();
NavigationService.NavigateAsync("LaunchPage");
return;
}
如有任何帮助,我们将不胜感激。我有 Xamarin、Xamarin Forms、Prism 和 Visual Studio 的最新更新,我不确定可能是什么问题。
您的 LaunchPage 抛出异常。更新 OnInitialized
使其更像这样:
protected override async void OnInitialized()
{
try
{
TaskScheduler.UnobservedTaskException += (sender, e) => {
Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
};
await NavigationService.NavigateAsync("LaunchPage");
}
catch(Exception e)
{
Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
}
}
这至少可以帮助您确定异常是什么。没有看到你的项目很难说,但我可能猜想你正在使用 IPlatformInitializer 并在 Android 而不是 iOS 上注册了一些东西,或者你依赖于 Xamarin Forms Dependency Service 并且存在依赖关系在 Android 但不是 iOS。
我刚刚在我的新应用程序中实现了 Prism 库,并且我正在使用 NavigationService。它在 Android 上运行良好,但是当我尝试在网络 Mac 上调试我的 Xamarin Forms iOS 应用程序时,我的 Main 方法抛出此异常:
Unhandled Exception:
Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch
Native stack trace:
0 CoreFoundation 0x00000001021f2b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010cdcf141 objc_exception_throw + 48
2 CoreFoundation 0x00000001021f6cf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x0000000102da93b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x0000000105ba0be6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 3343
5 UIKit 0x0000000105b9d793 -[UIApplication workspaceDidEndTransaction:] + 182
6 FrontBoardServices 0x000000010fae35f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
7 FrontBoardServices 0x000000010fae346d -[FBSSerialQueue _performNext] + 186
8 FrontBoardServices 0x000000010fae37f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
9 CoreFoundation 0x0000000102198c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
10 CoreFoundation 0x000000010217e0cf __CFRunLoopDoSources0 + 527
11 CoreFoundation 0x000000010217d5ff __CFRunLoopRun + 911
12 CoreFoundation 0x000000010217d016 CFRunLoopRunSpecific + 406
13 UIKit 0x0000000105b9c02f -[UIApplication _run] + 468
14 UIKit 0x0000000105ba20d4 UIApplicationMain + 159
15 ??? 0x0000000124f647fc 0x0 +
我的主要方法是这样的:
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
AppDelegate:
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
this.LoadApplication(new Core.CitizenApp());
return base.FinishedLaunching(app, options);
}
}
表单应用程序:
protected override void OnInitialized()
{
this.InitializeComponent();
NavigationService.NavigateAsync("LaunchPage");
return;
}
如有任何帮助,我们将不胜感激。我有 Xamarin、Xamarin Forms、Prism 和 Visual Studio 的最新更新,我不确定可能是什么问题。
您的 LaunchPage 抛出异常。更新 OnInitialized
使其更像这样:
protected override async void OnInitialized()
{
try
{
TaskScheduler.UnobservedTaskException += (sender, e) => {
Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
};
await NavigationService.NavigateAsync("LaunchPage");
}
catch(Exception e)
{
Logger.Log(e.Exception.ToString(), Category.Exception, Priority.High);
}
}
这至少可以帮助您确定异常是什么。没有看到你的项目很难说,但我可能猜想你正在使用 IPlatformInitializer 并在 Android 而不是 iOS 上注册了一些东西,或者你依赖于 Xamarin Forms Dependency Service 并且存在依赖关系在 Android 但不是 iOS。