为什么我的Xamarin PCL在为通用应用程序构建版本时抛出运行时异常

本文关键字:版本 构建 异常 运行时 应用程序 Xamarin 我的 PCL 为什么 | 更新日期: 2023-09-27 18:27:50

我有一个xamarin PCL,它在x86调试模式下构建良好。当我将其切换到发布模式(x86或x64)或x64调试时,我会得到运行时异常。可能与有关

https://forums.xamarin.com/discussion/57810/issue-with-xamarin-forms-in-windows-uwp-app-compiled-in-the-release-mode

但我不知道我在用什么其他组件。我该怎么说?

我的电脑是x64。当我在调试或发布中运行x64时,我会得到

MyApp.Interop.dll中的异常"System.NotImplementedException"。其他信息Arg_NotImplementedException。

在进入构造函数应用程序()之前。对构造函数的调用在这里:

LoadApplication(new MyApp.App());

当我构建x86时,我会做得更进一步。它进入MyAppConstructor并调用xaml构造函数,并给出异常:

System.Private.Reflection.Core.dll AdditionalInfo:Arg_InvokeMethodMissingMetadata,System.EventHandler中的System.Reflection.MissingMetadataException。有关详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=623485

所以它看起来像是我丢失的一个Xaml程序集。如何找到需要添加的程序集?

我把它放回了Debug,但把它改为"使用本机编译器",这样我就可以获得更多关于异常的详细信息:

x86:其他信息:无法在类型"System.EventHandler"上创建委托,因为它缺少Invoke方法的元数据。欲了解更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=616867

x64:Xamarin.Forms.Platform.UAP.dll中发生类型为"System.NotImplementedException"的异常,但未在用户代码中处理

附加信息:未实现该方法或操作。

更新:我猜Xamarin中不支持x64,因为没有移动产品具有x64处理器?x86版本仍然存在问题。我已经尝试在我的Universal App.xaml.cs 中添加以下程序集

  List<Assembly> assembliesToInclude = new List<Assembly>();
  assembliesToInclude.Add(typeof(MyApp.MyAppMainPage).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.ImageSource).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.StackLayout).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.Label).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.Button).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.FormattedString).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.Span).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.Image).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.ScrollView).GetTypeInfo().Assembly);
  assembliesToInclude.Add(typeof(Xamarin.Forms.WebView).GetTypeInfo().Assembly);
  // add this line
  Xamarin.Forms.Forms.Init(e,assembliesToInclude); // requires the `e` parameter

其中MyAppMainPage是我试图在PCL中加载的xaml页面,其余的是该页面所包含的UI元素。

我现在看到x86:抛出的异常

System.Private.Interop.dll中的"System.PlatformNotSupportedException"引发异常:System.Private.Threading.dll中的"System.AggregateException"引发异常:System.Private.Reflection.Core.dll 中的"System.Reflection.MissingMetadataException"

为什么不支持该平台?Xamarin支持环球,对吗?

为什么我的Xamarin PCL在为通用应用程序构建版本时抛出运行时异常

我添加了一个指令文件。添加一个以.rd.xml结尾的文件。我的是MyApp.rd.xml。然后包括异常所说的缺少的类型。我的是System.EventHandler。这是我的代码(您可能不需要另外两个)。

<?xml version="1.0" encoding="utf-8"?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
  <Type Name="MyApp.MyAppMainPage" Dynamic="Required All" /> 
  <Type Name="System.EventHandler" Dynamic="Required All" /> 
  <Namespace Name="System.Private.Reflection.Core" Serialize="Required All" />
</Application>
</Directives> 

我想在Xamarin的通用应用程序中,加载嵌入资源时需要包含程序集。我不得不更换

ImageSource.FromResource("MyApp.Images.Sign.jpg");

var assembly = typeof(MyAppMainPage).GetTypeInfo().Assembly;
ImageSource.FromResource("MyApp.Images.Sign.jpg",assembly);

您可以通过查看您拥有的资源

foreach (var res in assembly.GetManifestResourceNames())
  System.Diagnostics.Debug.WriteLine("found resource: " + res);

x64仍然损坏。