如何从外部 class 库(通过 NuGet 包)引用 UserControl?
How to reference a UserControl from an external class library (through a NuGet Package)?
是否可以在驻留在 UWP class 库中的 UWP 应用项目中引用和使用 UserControl?
我尝试在 class 库中创建一个 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 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 包引用将导致异常发生。库从来没有遇到过这种问题 classes... 但是,好吧,既然这是我想要实现的目标,我将重新表述这个问题。
我添加了解决方案架构的屏幕截图:
您的代码运行完美。尝试重新创建项目。
解决了这个link:https://dschenkelman.github.io/2014/06/25/windows-phone-8-1-nuget-packages-with-xaml-components/
当您拥有包含 XAML 组件的 NuGet 包时,您必须手动包含 XAML 二进制文件 (.xbf),否则解析将失败。
好吧,很高兴知道,但真让人头疼!
是否可以在驻留在 UWP class 库中的 UWP 应用项目中引用和使用 UserControl? 我尝试在 class 库中创建一个 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 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 包引用将导致异常发生。库从来没有遇到过这种问题 classes... 但是,好吧,既然这是我想要实现的目标,我将重新表述这个问题。 我添加了解决方案架构的屏幕截图:
您的代码运行完美。尝试重新创建项目。
解决了这个link:https://dschenkelman.github.io/2014/06/25/windows-phone-8-1-nuget-packages-with-xaml-components/
当您拥有包含 XAML 组件的 NuGet 包时,您必须手动包含 XAML 二进制文件 (.xbf),否则解析将失败。 好吧,很高兴知道,但真让人头疼!