gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:10:44 34 4
gpt4 key购买 nike

我有一个在 x86 Debug模式下构建良好的 xamarin PCL。当我将其切换到 Release模式(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。

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

LoadApplication(new MyApp.App());

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

System.Private.Reflection.Core.dll 附加信息中的 System.Reflection.MissingMetadataException:Arg_InvokeMethodMissingMetadata,System.EventHandler。如需更多信息,请访问 http://go.microsoft.com/fwlink/?LinkId=623485

所以它看起来像我缺少的 Xaml 程序集。我如何找出我需要添加的程序集?

我把它放回调试,但把它变成“使用 native 编译器”,这样我就可以获得有关异常的更多详细信息:

x86:附加信息:无法在类型“System.EventHandler”上创建委托(delegate),因为它缺少 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 支持 Universal 对吗?

最佳答案

我添加了一个指令文件。添加一个以 .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 仍然损坏。

关于c# - 为什么我的 Xamarin PCL 在为通用应用程序构建版本时抛出运行时异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34424184/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com