简体   繁体   English

“UWP class 库 dll 中的 XamlParseException

[英]"XamlParseException in UWP class library dll

I am working on a universal class library, targeted to use UWP apps.我正在开发一个通用 class 库,旨在使用 UWP 应用程序。

In this am trying to use Content Dialog to get some user input.在此我尝试使用内容对话框来获取一些用户输入。

All works good in debug, when I pack my library as dll and distribute, the ContentDialog not showing from the app which refers my dll.在调试中一切正常,当我将我的库打包为 dll 并分发时,ContentDialog 没有从引用我的 dll 的应用程序中显示。

I am getting Windows.UI.Xaml.Markup.XamlParseException: XAML parsing failed exception, i got this through log file.我收到Windows.UI.Xaml.Markup.XamlParseException: XAML parsing failed异常,我通过日志文件得到了这个。

Here is my code这是我的代码

ContentDialog内容对话框

<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内容对话框.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);
    }
}

And am creating a new instance and try to show the dialog, like this我正在创建一个新实例并尝试显示对话框,就像这样

 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;
    }

Everything works good, if I refer this library in code base.如果我在代码库中引用这个库,一切都很好。 If I get release dll and refer it from a test app, am getting the xaml parse exception.如果我获得版本 dll 并从测试应用程序中引用它,则会收到 xaml 解析异常。

Can anyone help me in this.任何人都可以在这方面帮助我。

Thanks in advance.提前致谢。

Everything works good, if I refer this library in code base.如果我在代码库中引用这个库,一切都很好。 If I get release dll and refer it from a test app, am getting the xaml parse exception.如果我获得版本 dll 并从测试应用程序中引用它,则会收到 xaml 解析异常。

Great question, the problem is your dll file miss Xaml Content .好问题,问题是您的 dll 文件缺少Xaml 内容 When you compile a dll with xaml file in it, it will be recorded into xxxx.xr.xml files, and those files must be copied as well to the bin directory ( BUT NOT Obj folder )of your app with relative path.当你编译一个带有 xaml 文件的 dll 时,它会被记录到 xxxx.xr.xml 文件中,并且这些文件必须复制到你的应用程序目录中,而不是你的bin路径。 After build the class library, please check if the bin folder contains dll, pdb, pri and dll resource folder like the following.构建 class 库后,请检查 bin 文件夹是否包含 dll、pdb、pri 和 dll 资源文件夹,如下所示。

For the testing, it will work, if you directly add the dll file where in the class library bin folder to your project reference.对于测试,如果您直接将 class 库 bin 文件夹中的 dll 文件添加到您的项目参考中,它将起作用。

在此处输入图像描述

Finally, I found the solution.最后,我找到了解决方案。

Thanks @Nico for the answer, its almost answered the question.感谢@Nico 的回答,它几乎回答了这个问题。

Here is the link which gives you the clear picture about the issue这是为您提供有关该问题的清晰图片的链接

Missing xaml.xr of a class library file in UWP UWP 中缺少 class 库文件的 xaml.xr

Steps脚步

1) Check "Generate library layout" in your project properties 1)在项目属性中选中“生成库布局” 在此处输入图像描述

2) While copy your dll from bin/release folder, copy these files too 2) 从 bin/release 文件夹中复制 dll 时,也复制这些文件

  • ClassLibrary1(Class Library name) Folder ClassLibrary1(类库名称)文件夹

    1. ClassLibrary1.xr.xml ClassLibrary1.xr.xml

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

  • ClassLibrary1.dll类库1.dll

  • ClassLibrary1.pri ClassLibrary1.pri

Keep all these files in the same folder where you keep your library dll.将所有这些文件保存在您保存库 dll 的同一文件夹中。

Just refer your library dll alone to the referrer project.只需将您的库 dll 单独引用到引用项目即可。

All other files will be automatically referred.所有其他文件将被自动引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM