【问题标题】:"XamlParseException in UWP class library dll“UWP 类库 dll 中的 XamlParseException
【发布时间】:2020-04-03 14:15:49
【问题描述】:

我正在开发一个通用类库,旨在使用 UWP 应用程序。

在此我尝试使用内容对话框来获取一些用户输入。

在调试中一切正常,当我将库打包为 dll 并分发时,ContentDialog 没有从引用我的 dll 的应用程序中显示。

我收到Windows.UI.Xaml.Markup.XamlParseException: XAML parsing failed 异常,我是通过日志文件得到的。

这是我的代码

内容对话框

<ContentDialog
x:Class="xxxx.yyyy.InputContentDialogue"
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"
x:Name="dialog"
Title="Title">

<ContentDialog.Resources>
    <Style x:Name="ButtonStyleNoTabFocus" TargetType="Button">
        <Setter Property="FocusVisualPrimaryBrush" Value="Transparent" />
        <Setter Property="Margin" Value="5"/>
    </Style>
</ContentDialog.Resources>

<!-- Content body -->
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,20" MinWidth="550">

    <StackPanel Orientation="Vertical">

        <TextBlock  TextWrapping="WrapWholeWords" Margin="5,0,0,10">
       shkgdsakjfdhgsajkfdhkasd sadkfjahsdkj asdfjasfdja asdkfjasdf asdkjfnas asdkjfnasd
        </TextBlock>

        <TextBlock Margin="5,0,0,10">sjkdhfkjsdf sdajfakjdsb sadfkajsdfa.
        </TextBlock>

        <StackPanel Orientation="Horizontal">
            <Button  TabIndex="0" 
                 HorizontalAlignment="Center" 
                 Content="hey there"
                 Style="{StaticResource ButtonStyleNoTabFocus}" 
                 x:Name="btn1" 
                 Click="btn1_Click" 
                 GotFocus="Btn_GotFocus"
                 LostFocus="Btn_LostFocus"/>
            <Button  HorizontalAlignment="Center" 
                 Content="Hi" 
                 x:Name="btn2" 
                 Style="{StaticResource ButtonStyleNoTabFocus}" 
                 Click="btn2_Click"
                 GotFocus="Btn_GotFocus"
                 LostFocus="Btn_LostFocus"/>
            <Button  HorizontalAlignment="Center" 
                 Content="Hello" 
                 Style="{StaticResource ButtonStyleNoTabFocus}" 
                 x:Name="btn3" 
                 Click="btn3_Click" 
                 GotFocus="Btn_GotFocus"
                 LostFocus="Btn_LostFocus"/>
        </StackPanel>
    </StackPanel>
</Grid>

ContentDialog.cs

public sealed partial class InputContentDialogue : ContentDialog
{

    public UserConsentContentDialogue()
    {
        this.InitializeComponent();
        this.Result = -1;
        this.Closing += ContentDialogue_Closing;
    }


    private void ContentDialogue_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
    {
        if (args.Result == ContentDialogResult.None && this.Result == -1)
        {
            args.Cancel = true;
        }
    }

    public int Result { get; set; }


    // Handle the button clicks from dialog
    private void btn1_Click(object sender, RoutedEventArgs e)
    {

        this.Result = 0;
        // Close the dialog
        dialog.Hide();
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
        this.Result = 1;
        // Close the dialog
        dialog.Hide();
    }

    private void btn3_Click(object sender, RoutedEventArgs e)
    {
        this.Result = 2;
        // Close the dialog
        dialog.Hide();
    }

    private void Btn_GotFocus(object sender, RoutedEventArgs e)
    {
        Brush _blinkBrush = Application.Current.Resources["SystemControlHighlightAccentBrush"] as SolidColorBrush;
        (sender as Button).BorderBrush = _blinkBrush;
    }

    private void Btn_LostFocus(object sender, RoutedEventArgs e)
    {
        (sender as Button).BorderBrush = new SolidColorBrush(Colors.Transparent);
    }
}

我正在创建一个新实例并尝试显示对话框,就像这样

 internal static async Task<int> ShowMyContentDialog()
    {
        try
        {
            InputContentDialogue dialogue = new InputContentDialogue();

            await dialogue.ShowAsync();

            return dialogue.Result;
        }
        catch(Exception e)
        {
            FileOperations.WriteToLogFile("ERROR occurred "+ e.ToString());
        }
        return -1;
    }

如果我在代码库中引用这个库,一切都很好。 如果我获得发布 dll 并从测试应用程序引用它,则会收到 xaml 解析异常。

谁能帮帮我。

提前致谢。

【问题讨论】:

    标签: c# xaml dll uwp xamlparseexception


    【解决方案1】:

    如果我在代码库中引用这个库,一切都很好。如果我获得发布 dll 并从测试应用程序引用它,则会收到 xaml 解析异常。

    很好的问题,问题是您的 dll 文件缺少 Xaml 内容。当你编译一个带有 xaml 文件的 dll 时,它会被记录到 xxxx.xr.xml 文件中,这些文件也必须复制到 bin 目录(BUT NOT Obj 文件夹) 的相对路径。构建类库后,请检查 bin 文件夹是否包含 dll、pdb、pri 和 dll 资源文件夹,如下所示。

    为了测试,直接将类库bin文件夹中的dll文件添加到项目引用中即可。

    【讨论】:

      【解决方案2】:

      终于找到了解决办法。

      感谢@Nico 的回答,它几乎回答了这个问题。

      这个链接可以让你清楚地了解这个问题

      Missing xaml.xr of a class library file in UWP

      步骤

      1) 在项目属性中勾选“生成库布局”

      2) 从 bin/release 文件夹中复制 dll 时,也要复制这些文件

      • ClassLibrary1(类库名称)文件夹

        1. ClassLibrary1.xr.xml

          2.UserControl.xaml(用户控件 XAML 文件)

      • ClassLibrary1.dll

      • ClassLibrary1.pri

      将所有这些文件保存在您保存库 dll 的同一文件夹中。

      只需将您的库 dll 单独引用到引用项目。

      所有其他文件将被自动引用。

      【讨论】:

        猜你喜欢
        • 2021-02-08
        • 1970-01-01
        • 2019-06-04
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多