Xamarin.UWP 启动期间未处理的异常

Xamarin.UWP Unhandled Exception during launch

我的 objective 是用 UWP.

扩展 existing Xamarin project

我有两个共享项目,X.mobile.shared 和 X.application.shared,所以我添加了Uwp 项目参考这两个项目。

按照从 the documentation, I have also installed all the Nuget packages available in these shared projects as it's noted in the answer of these issue 添加 uwp 的步骤后。

现在,当我调试 UWP 项目时,除了 未处理的异常 ,我没有收到任何错误,我试图指出触发该异常的线索,我发现取消注释这一行:

 rootFrame.Navigate(typeof(MainPage), e.Arguments);

触发未处理的异常:

如图所示,邮件包含:

    Message "Object reference not set to an instance of an object." 

这意味着存在对空对象的引用!

我做错了什么?

更新

由于线路上的断点触发了错误,我试图查看该方法的值是参数,我发现 DeclaringMethodtypeofMain( ) 返回 "SystemInvalidOpertaionException"

这是什么意思?

更新 2

听取@Martin Zikmund 的建议,我检查了 Stack trace,发现问题发生在:

  Acme.PhoneBookDemo.Core.Dependency.DependencyResolver.Resolve[T]()\r\n at Acme.PhoneBookDemo.App.OnStart()

回到那个class,这是OnStart()方法的代码:

 protected override async void OnStart()
    {
        base.OnStart();

        if (Device.RuntimePlatform == Device.iOS)
        {
            SetInitialScreenForIos();
            await UserConfigurationManager.GetIfNeedsAsync();
        }

        await DependencyResolver.Resolve<INavigationService>().InitializeAsync();

        OnResume();
    }

调试器到达此行时触发问题:

 await DependencyResolver.Resolve<INavigationService>().InitializeAsync();

根据 DependencyResolver.Resolve<T> 调用期间发生的异常。 DependencyResolver 是一个 class in Xamarin.Forms 作为 "service locator".

我无权访问源代码,但这意味着 UWP 项目中未提供需要特定于平台的实现的服务。

检查异常的堆栈跟踪,您应该能够看到依赖项丢失的位置,然后能够判断需要什么 implemented.I 建议您尤其应该查看 ViewModel 加载的第一个视图,因为那里似乎需要服务(应用程序在启动时立即崩溃)。

有关依赖注入如何在 Xamarin.Forms 中工作的详细信息,请参阅 Docs

我收到了 xamarin-docs 's github 的回复,告诉我为了使用 UWP 扩展您现有的 UWP 项目,您在解决方案中使用的所有 nuget 包都应该支持 UWP(不是只有 .shared).

中存在的那些

在我的解决方案中,我有许多不支持 UWP 的包,例如:

ACRSupport 仅支持 android 和 ios

总之,aspnetzero 的项目无法扩展为使用 UWP。

答案更新:

我组织的一位同事通过应对 *.droid 项目的实施找到了解决方案。

这个答案对 aspBoilerplate 用户很有用,特别是 aspnetzero 用户,他想用 扩展他的项目UWP.

这是使其工作的步骤:

  1. 第一,关注this tutorial
  2. 如果您 运行 在第一步之后进行预测,您可能会对“未处理的异常=对象感到惊讶引用未设置到对象的实例。”,这是因为依赖注入容器(又名服务定位器)无法找到您的 uwp 注册 16=], 所以:

    2.1。为您添加的 Uwp 项目定义一个模块 (Add>NewItem>PhoneBookDemoXamarinUwpModule.cs):

     using Abp.Modules;
     using Abp.Reflection.Extensions;
    
       namespace Acme.PhoneBookDemo
       {
           [DependsOn(typeof(PhoneBookDemoXamarinSharedModule))]
              public class PhoneBookDemoXamarinUwpModule : AbpModule
              {
                public override void Initialize()
                {     IocManager.RegisterAssemblyByConvention(typeof(PhoneBookDemoXamarinUwpModule).GetAssembly());
                }
            }
        }
    

    在上面我们注册依赖的initialize方法中,IocManager用于注册模块class.

  3. App.xaml.csOnLaunched() 方法,添加以下行:

    //rootFrame.NavigationFailed += OnNavigationFailed;
    
      ApplicationBootstrapper.InitializeIfNeeds<PhoneBookDemoXamarinUwpModule>();
          await StartUpApplicationAsync();
    
     //Xamarin.Forms.Forms.Init(e); // requires the `e` parameter
    

    然后添加这个方法:

     private async Task StartUpApplicationAsync()
     {
        await UserConfigurationManager.GetIfNeedsAsync();
      }
    
    private static void ConfigureFlurlHttp()
      {
        FlurlHttp.Configure(c =>
         {
            c.HttpClientFactory = new ModernHttpClientFactory();
         });
    
      }
    
  4. 将这些 Nuget 包添加到您的 Uwp 项目中:

    Xamarin.forms(确保它与您的 *.Mobile.shared 项目的版本相同)

    Win2D.uwp

  5. 最后,从您的 uwp 项目中,添加对 Acme.PhoneBook.Mobile.sharedAcme.PhoneBook.Application.shared 的引用 & Acme.PhoneBook.Application.Core

就是这样,它对你来说 运行,就像对我一样