简体   繁体   English

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

[英]Xamarin.UWP Unhandled Exception during launch

My objective is to extend an existing Xamarin project with UWP . 我的目标是使用UWP扩展现有的Xamarin项目

I have two shared projects , X. mobile.shared and X. application.shared , so i added reference for the Uwp project to this two project. 我有两个共享项目,十mobile.shared和X. application.shared,所以我对UWP项目这两项目添加引用。

After following the steps to add uwp from 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的步骤进行操作之后,我还安装了这些共享项目中所有可用的Nuget软件包,正如在回答这些问题中所指出的那样。

Now when I debug the UWP project, I don't get any error but an unhandled exception , I tried to pin point the ligne that trigger that exception and I found that uncommenting this line: 现在,当我调试UWP项目时, 除了未处理的异常外 ,我没有得到任何错误,我试图指出触发该异常的ligne,然后发现取消注释此行:

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

Triggers the unhandled exception: 触发未处理的异常:

未处理的异常

The message contains,as you see in the image: 如该图所示,该消息包含:

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

which means that there is a referencing to a null object ! 这意味着存在对空对象的引用!

What am I doing wrong ? 我究竟做错了什么 ?

Update 更新

With a BreakPoint on the ligne triggering the error , i tried to see the value of the method is arguments , i found that DeclaringMethod called by typeofMain() is returning " SystemInvalidOpertaionException " . 随着对LIGNE触发错误断点,我试图看到该方法的值是参数,我发现,通过DeclaringMethod称为typeofMain()被返回“SystemInvalidOpertaionException” SystemInvalidOperationException

what that means ? 那是什么意思 ?

Update 2 更新2

Getting the advice of @Martin Zikmund , i checked the Stack trace and i found that the problem occurs at : 得到@Martin Zikmund的建议后,我检查了堆栈跟踪,发现问题发生在:

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

back to that class, this is the code of OnStart() method: 回到该类,这是OnStart()方法的代码:

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

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

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

        OnResume();
    }

the problem is triggred when the debugger arrive at this line: 当调试器到达以下行时,将触发该问题:

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

According to the exception it occurs during DependencyResolver.Resolve<T> call. 根据异常,它发生在DependencyResolver.Resolve<T>调用期间。 DependencyResolver is a class in Xamarin.Forms which acts as a "service locator". DependencyResolverXamarin.Forms中的一个 ,充当“服务定位器”。

I don't have access to the source code, but this means there is a service that requires a platform-specific implementation which is not provided in the UWP project. 我无权访问源代码,但这意味着有一项服务需要特定于平台的实现,而UWP项目中未提供该服务。

Check the Stack Trace for the Exception and you should be able to see where the dependency was missing and then be able to tell what needs to be implemented.I suggest you should look especially in the ViewModel of the first view that is loaded as it seems the service is required there (app crashes immediately on launch). 检查堆栈跟踪中的异常,您应该能够看到缺少依赖项的地方,然后能够告诉我们需要实现什么。我建议您应该特别看一下加载的第一个视图的ViewModel该服务是必需的(应用在启动时立即崩溃)。

For details on how Dependency injection works in Xamarin.Forms see the Docs . 有关Xamarin.Forms中依赖注入的工作方式的详细信息,请参阅文档

I got a response from xamarin-docs 's github , telling that in order to extend your existing UWP project with UWP all the nuget packages you use in your solution should have a support to UWP (not only the ones that exists in .shared ). 我收到了xamarin-docs的github的回复,告诉我们为了使用UWP扩展现有的UWP项目,您在解决方案中使用的所有nuget软件包都应支持UWP(不仅是.shared中存在的软件包 )。 。

In my solution i have many packages that dosen't support UWP, example : 在我的解决方案中,我有很多不支持UWP的软件包,例如:

ACRSupport supports only android and ios ACRSupport仅支持android和ios

In conclusion, aspnetzero 's project can't be extended to use UWP. 总之,aspnetzero的项目无法扩展为使用UWP。

UPDATE OF ANSWER: 答案更新:

A co-worker in my organisation found a solution by coping the implementation of *.droid project. 我组织中的一位同事通过应对* .droid项目的实施找到了解决方案。

This answer is useful for aspBoilerplate user, in particular aspnetzero user, who want to extend his project with UWP . 该答案对于想要用UWP扩展其项目的aspBoilerplate用户(特别是aspnetzero用户)很有用。

This are the steps , to make it work : 这是使它起作用的步骤:

  1. First ,follow this tutorial 首先,按照本教程
  2. If you run you project after the first step , you may be surprised by an " unhandled exception = Object reference not set to an instance of an object. ", this happens because the Dependancy Injection Container(aka service locator) can't find the registration of your uwp abpmodule , So : 如果在第一步之后运行项目,则可能会因“ 未处理的异常 = 对象引用未设置为对象的实例。 ”而感到惊讶,这是因为依赖注入容器(又名服务定位器)找不到注册您的uwp abpmodule ,所以:

    2.1. 2.1。 Define a Module for your add Uwp project (Add>NewItem>PhoneBookDemoXamarinUwpModule.cs): 为添加的Uwp项目定义一个模块(添加> 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()); } } } 

    In the initialize method above we register dependencies , IocManager is used to register the module class. 在上面的initialize方法中,我们注册依赖项,IocManager用于注册模块类。

  3. in App.xaml.cs 's OnLaunched() method , add these lines : App.xaml.csOnLaunched()方法中,添加以下行:

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

    then add this methods: 然后添加以下方法:

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

    Xamarin.forms (ensure it's the same version with your *.Mobile.shared project) Xamarin.forms (确保与您的* .Mobile.shared项目的版本相同)

    Win2D.uwp Win2D.uwp

  5. Finally from your uwp project, add a reference to Acme.PhoneBook.Mobile.shared , Acme.PhoneBook.Application.shared & Acme.PhoneBook.Application.Core 最后,从uwp项目中,添加对Acme.PhoneBook.Mobile.sharedAcme.PhoneBook.Application.sharedAcme.PhoneBook.Application.Core的引用

And that is it , it would run for you , as it did to me 就是这样,它将为您运行,就像对我一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM