How to reference a UserControl from an external class library (through a NuGet Package)?

时间:2016-01-12 11:22:07

标签: c# xaml nuget uwp

Is it possible to reference and use a UserControl in UWP app project that resides in a UWP class library? I tried creating a UserControl in a class library but when I try to use it in the app I get:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe but was not handled in user code

WinRT information: Cannot locate resource from 'ms-appx:///ClassLibrary1/MyUserControl1.xaml'. [Line: 10 Position: 6]

Edit: Sample Page where I'm trying to use the 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>

This is the UserControl in 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>

EDIT 2: I didn't thought this could make such a difference, but... Standard reference to the library works without error, referencing through NuGet Package will cause the exception to happen. Never had this kind of issue with libraries with just plain classes... but, well, since is this what I'm trying to achieve I'll reformulate the question. I added a screenshot of the solution schema:

Solution

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您的代码完美无缺。尝试重新创建项目。

相关问题