Xamarin.UWP在发布期间出现未处理的异常

问题描述 投票:0回答:2

我的目标是用UWP扩展existing Xamarin project

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

按照从the documentation添加uwp的步骤之后,我还安装了这些共享项目中可用的所有Nuget包,如these issue的答案中所述。

现在,当我调试UWP项目时,我没有得到任何错误,但是未处理的异常,我试图指出触发该异常的ligne并且我发现取消注释这一行:

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

触发未处理的异常:

unhandled exception

如图所示,该消息包含:

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

这意味着有一个引用null对象!

我究竟做错了什么 ?

Update

使用ligne上的BreakPoint触发错误,我试图看到方法的值是参数,我发现typeofMain()调用的DeclaringMethod返回"SystemInvalidOpertaionException"SystemInvalidOperationException

那意味着什么 ?

Update 2

得到@Martin Zikmund的建议,我检查了堆栈跟踪,我发现问题发生在:

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

回到那个类,这是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();
c# xamarin xamarin.forms uwp aspnetboilerplate
2个回答
1
投票

根据例外,它发生在DependencyResolver.Resolve<T>呼叫期间。 DependencyResolver是一个class in Xamarin.Forms,充当“服务定位器”。

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

检查堆栈跟踪是否有异常,你应该能够看到缺少依赖的位置,然后能够告诉需要实现的内容。我建议你应该特别注意第一个加载的视图的ViewModel那里需要该服务(应用程序在启动时立即崩溃)。

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


0
投票

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

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

ACRSupport仅支持android和ios

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

UPDATE OF ANSWER:

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

这个答案对于希望用UWP扩展他的项目的aspBoilerplate用户,特别是aspnetzero用户非常有用。

这是使其工作的步骤:

  1. 首先,请关注this tutorial
  2. 如果你在第一步之后运行你的项目,你可能会惊讶于“未处理的异常=对象引用没有设置为对象的实例。”,这是因为Dependancy Injection Container(aka服务定位器)找不到注册你的uwp abpmodule,所以: 2.1。为add 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用于注册模块类。
  3. 在App.xaml.cs的OnLaunched()方法中,添加以下行: //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.shared,Acme.PhoneBook.Application.shared和Acme.PhoneBook.Application.Core的引用。

就是这样,它会为你而战,就像它对我一样

© www.soinside.com 2019 - 2024. All rights reserved.