如何从外部类库(通过 NuGet 包)引用 UserControl?

2024-05-10

是否可以在驻留在 UWP 类库中的 UWP 应用项目中引用和使用 UserControl? 我尝试在类库中创建 UserControl,但是当我尝试在应用程序中使用它时,我得到:

App1.exe 中发生“Windows.UI.Xaml.Markup.XamlParseException”类型的异常,但未在用户代码中处理

WinRT 信息:无法从“ms-appx:///ClassLibrary1/MyUserControl1.xaml”找到资源。 [线路:10 位置:6]

编辑: 我尝试使用 MyUserControl1 的示例页面:

<Page x:Class="App.MainPage"
  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:lib="using:ClassLibrary1"
  xmlns:local="using:App"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d">

    <Grid>
        <lib:MyUserControl1 />
    </Grid>

</Page>

这是ClassLibrary1中的UserControl

<UserControl x:Class="ClassLibrary1.MyUserControl1"
         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:local="using:ClassLibrary1"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d">

    <Grid />
</UserControl>

编辑2:我没想到这会产生如此大的影响,但是...... 对库的标准引用不会出现错误,通过 NuGet Package 引用将导致异常发生。对于只有普通类的库,从来没有遇到过这种问题……但是,好吧,既然这是我想要实现的目标,我将重新阐述这个问题。 我添加了解决方案架构的屏幕截图:


感谢此链接解决:https://dschenkelman.github.io/2014/06/25/windows-phone-8-1-nuget-packages-with-xaml-components/ https://dschenkelman.github.io/2014/06/25/windows-phone-8-1-nuget-packages-with-xaml-components/

当您拥有带有 XAML 组件的 NuGet 包时,您必须手动包含 XAML 二进制文件 (.xbf),否则解析将失败。 嗯,很高兴知道,但真是令人头疼!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从外部类库(通过 NuGet 包)引用 UserControl? 的相关文章

随机推荐