Xamarin表单UWP - 无法编译.NET Native工具链

问题描述 投票:1回答:3

我们有一个在没有.NET Native工具链的情况下编译好的应用程序,但是当我们单击此复选框时,我们会收到这些编译错误(Debug或Release):

*Severity   Code    Description Project File    Line    Suppression State
Error       at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelper(IEnumerable`1 contracts, IEnumerable`1 jsonContracts, GeneratorSettings settings, String intermediateAssembly, IEnumerable`1 wcfSerializers) Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.Main(String[] args)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       ILT0032: Failed to compile serialization code. See the build log for error details. Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelperCode(IEnumerable`1 contracts, IEnumerable`1 jsonContracts, IEnumerable`1 wcfSerializers)  Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.AddKnownContractsLists(McgCodeTypeDeclaration container, ContractTables tables)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691*    

输出窗口中存在很多错误。这里发布的内容太多了,但这里有几个提示:

*1>  C:\AdaptSource\Xivic\Adapt.Presentation.Helpdesk.UWP\obj\x86\Debug\ilc\in\System.Reflection.Extensions.dll
1>  The assembly 'System.Reflection.Metadata.dll' is found in more than one folder.
1>  file:///C:/AdaptSource/Xivic/Adapt.Presentation.Helpdesk.UWP/obj/x86/Debug/ilc/in/System.Reflection.Metadata.dll*

我认为也许有浮动的DLL,并且对于使用哪一个感到困惑,但即使在完整的Git清理之后仍然会发生这种情况。

这是Default.rd.xml

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <!--
      An Assembly element with Name="*Application*" applies to all assemblies in
      the application package. The asterisks are not wildcards.
    -->
    <Assembly Name="*Application*" Dynamic="Required All" />


    <!-- Add your application specific runtime directives here. -->


  </Application>
</Directives>

从我所理解的一切来看,这应包括我们解决方案中的所有类型。这个假设是否正确?

我觉得它可以为我们项目中存在的类型指定元数据。这里有一篇文章提示:https://docs.microsoft.com/en-us/dotnet/framework/net-native/getting-started-with-net-native。但是,我真的不明白它要求我做什么,所以我被困住了。

c# uwp xamarin.uwp .net-native
3个回答
1
投票

这可能已经解决了,但你的应用程序是否有可能使用像DataContractJsonSerializer或XmlSerializer这样的序列化类进行一些序列化?

我有类似的构建错误,我不得不在我的Default.rd.xml中添加它们:

<Namespace Name="My.Namespace.Model" DataContractJsonSerializer="Required All" />
<Namespace Name="My.Namespace.Sql" XmlSerializer="Required All" />

对于Forms.Init上的uwp特定覆盖,我不必包括除下面的插件程序集之外的任何其他程序集(但是如果没有它,这不会导致任何构建/编译错误 - 只有缺少字体图标的运行时问题):

List<Assembly> assembliesToInclude = new List<Assembly>();
assembliesToInclude.Add(typeof(FormsPlugin.Iconize.IconButton).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(FormsPlugin.Iconize.UWP.IconControls).GetTypeInfo().Assembly);
Xamarin.Forms.Forms.Init(e, assembliesToInclude);

0
投票

如果您的UWP应用程序引用了多个程序集(例如第三方控件库,或者您的应用程序本身被拆分为多个PCL),则Xamarin.Forms可能无法从这些程序集(例如自定义渲染器)加载对象。

使用Compile with .NET Native工具链时可能会出现这种情况,该工具链是项目的Properties> Build> General窗口中UWP apps的一个选项。

你可以通过在App.xaml.cs中使用Forms.Init调用的UWP特定重载来解决这个问题,如下面的代码所示(你应该用你的代码引用的实际类替换ClassInOtherAssembly):

// you'll need to add `using System.Reflection;`
List<Assembly> assembliesToInclude = new List<Assembly>();

//Now, add in all the assemblies your app uses
assembliesToInclude.Add(typeof (ClassInOtherAssembly).GetTypeInfo().Assembly);

//Also do this for all your other 3rd party libraries

Xamarin.Forms.Forms.Init(e, assembliesToInclude);
// replaces Xamarin.Forms.Forms.Init(e);

添加对应用程序引用的每个程序集的引用。有关更多信息,请参阅Target Invocation Exception


-2
投票

在项目的“属性”>“构建”>“常规”窗口中使用Compile with .NET Native tool chain时,可能会出现这种情况,这是UWP应用程序的一个选项。

尝试取消选中此选项。

什么Nico说应该根据this article工作。

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