我的目标是用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);
触发未处理的异常:
如图所示,该消息包含:
Message "Object reference not set to an instance of an object."
这意味着有一个引用null对象!
我究竟做错了什么 ?
使用ligne上的BreakPoint触发错误,我试图看到方法的值是参数,我发现typeofMain()调用的DeclaringMethod返回"SystemInvalidOpertaionException"。
那意味着什么 ?
得到@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();
根据例外,它发生在DependencyResolver.Resolve<T>
呼叫期间。 DependencyResolver
是一个class in Xamarin.Forms,充当“服务定位器”。
我无权访问源代码,但这意味着有一项服务需要UWP项目中未提供的特定于平台的实现。
检查堆栈跟踪是否有异常,你应该能够看到缺少依赖的位置,然后能够告诉需要实现的内容。我建议你应该特别注意第一个加载的视图的ViewModel
那里需要该服务(应用程序在启动时立即崩溃)。
有关依赖注入如何在Xamarin.Forms中工作的详细信息,请参阅Docs。
我得到了xamarin-docs 's github的回复,告诉我们为了用UWP扩展你现有的UWP项目,你在解决方案中使用的所有nuget包都应该支持UWP(不仅是.shared中存在的那些)。
在我的解决方案中,我有许多不支持UWP的软件包,例如:
ACRSupport仅支持android和ios
总之,aspnetzero的项目无法扩展到使用UWP。
我的组织中的一位同事通过应对* .droid项目的实现找到了解决方案。
这个答案对于希望用UWP扩展他的项目的aspBoilerplate用户,特别是aspnetzero用户非常有用。
这是使其工作的步骤:
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用于注册模块类。//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();
});
}
就是这样,它会为你而战,就像它对我一样