uwp xaml 解析失败,类库 dll

Muh*_*eef 6 c# xaml xamlparseexception windows-runtime uwp

我有一个非常简单的 uwp 应用程序,其中我引用了一个类库,该类库显然也是一个 uwp 项目,并且具有自定义 ContentDialog。当我直接将它作为项目引用时,它工作得很好,并且 ContentDialog 也会打开。但是,当我删除项目并使用其生成的 dll(用于调试模式的 Debug 和用于发布模式的 Release)并引用该 dll 时,我会在该 ContentDialog 的构造函数中收到 xaml Parse 异常。

UWP 客户端应用程序代码

public sealed partial class MainPage : Page
{
    private async Task Test()
    {
        var exitNode = new ExitNodeCode.ExitNode();
        await exitNode.AskForPermissionPopup();
    }

    public MainPage() => InitializeComponent();

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        await Test();
        base.OnNavigatedTo(e);  
    }
}
Run Code Online (Sandbox Code Playgroud)

Test()方法上抛出异常,但堆栈跟踪(用断点确认)导致该自定义 contentDialog 的构造函数中的 InitializeComponent() 方法。

类库项目中的方法

public async Task AskForPermissionPopup()
{
    var dialog = new PermissionDialog();
    await dialog.ShowAsync();                
}
Run Code Online (Sandbox Code Playgroud)

自定义内容对话框的 xaml

<ContentDialog
    x:Class="ExitNodeCode.PermissionDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    PrimaryButtonText="I Agree!"
    SecondaryButtonText="Maybe Later"
    PrimaryButtonClick="PermissionDialog_PrimaryButtonClick"
    SecondaryButtonClick="PermissionDialog_SecondaryButtonClick">
    <Grid >           
    </Grid>
</ContentDialog>
Run Code Online (Sandbox Code Playgroud)

对话框的cs代码

public sealed partial class PermissionDialog : ContentDialog
{
    public PermissionDialog()
    {
        InitializeComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

类库项目被“windows 运行时组件”项目引用,该项目是一个后台任务,客户端应用程序引用了这个后台任务,但我认为这在这里无关紧要,因为这是一个 xaml 解析异常,并且后台任务甚至没有注册发生异常

Mat*_*den 1

如果您在 ARM64 目标上看到此问题,则可以通过将其添加到您的项目中来解决此问题:

<ProperytGroup>
    <!-- ARM64 builds for managed apps use .NET Native. We can't use the Reflection Provider for that. -->
    <EnableTypeInfoReflection Condition="'$(Configuration)' == 'ARM64'">false</EnableTypeInfoReflection>
</ProperytGroup>
Run Code Online (Sandbox Code Playgroud)

本质上,XAML/.NET 层在 ARM64 上的工作方式不同,并且该边界存在问题。我的理解是 Windows SDK 人员正在努力解决这个问题。