【问题标题】:How to reference a UserControl from an external class library (through a NuGet Package)?如何从外部类库(通过 NuGet 包)引用 UserControl?
【发布时间】:2016-01-12 11:22:06
【问题描述】:

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

【问题讨论】:

  • 发布 XAML,其中使用了 UserControl1
  • 你是这样在命名空间中指定程序集的吗? stackoverflow.com/a/3297719/1328536
  • @fuchs777 也尝试以这种方式指定命名空间,XAML 解析仍然失败
  • 您是否在您的其他项目(存在此页面)中添加了对项目ClassLibrary1 的引用?
  • @TomWuyts 是的,我只有引用 ClassLibrary1 的 App1 项目。这是一个测试,看看我是否可以从外部库中引用自定义控件,但似乎这无法完成,或者我遗漏了一些东西

标签: c# xaml nuget uwp


【解决方案1】:

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

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

【讨论】:

  • 感谢您的见解。实际上,即使在 2020 年,我也面临着这个问题,但似乎没有很好的教程。根据您的意见,我编写了详细的步骤来帮助自己和未来遇到类似问题的其他人:cuteprogramming.wordpress.com/2020/04/26/…
【解决方案2】:

您的代码运行良好。尝试重新创建项目。

【讨论】:

  • 是的,它使用标准引用完美运行,我忘记指定我想要实现的是使用 NuGet 包对库(带有自定义 xaml 控件)的引用。这样,异常就出现了。老实说,我不认为这将是一个重要的细节,也不是问题的根源:P 我在问题中添加了细节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多