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

标签 c# xaml nuget uwp

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

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

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 包引用将导致异常发生。对于只有普通类的库,从来没有遇到过这种问题……但是,好吧,既然这是我想要实现的目标,我将重新表述这个问题。 我添加了解决方案架构的屏幕截图:

Solution

最佳答案

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

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

关于c# - 如何从外部类库(通过 NuGet 包)引用 UserControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34742349/

相关文章:

c# - 为什么我不能在我的结构中初始化我的字段?

c# - Excel Interop : reference by number, 不是字符串

c# - 获取设置为自动的网格的实际宽度

c# - WPF TreeView : Skip a Level

c# - 将 Xaml 路径转换为 ​​C# 代码

asp.net-mvc - StructureMap-MVC3 中 scan.TheCallingAssembly、scan.WithDefaultConventions 的用途

c# - MonoGame 管道工具是否不再包含在 Visual Studio MonoGame NuGet 包中?

c# - 使用 System.Data.Linq.Mapping 并自动递增 sqlite 数据库中的主键时出错

c# - 如何使用 ZXing C# 端口

visual-studio-2013 - 来自 Visual Studio 2013 中 Team City nuget feed 的通知