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

你。 提交于 2019-12-21 19:57:03

问题


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:


回答1:


Solved thanks to this link: https://dschenkelman.github.io/2014/06/25/windows-phone-8-1-nuget-packages-with-xaml-components/

When you have NuGet packages with XAML componenents you must include manually the XAML binary files (.xbf) otherwise the parsing will fail. Well, good to know but what a headache!




回答2:


Your code works perfectly. Try to create project anew.



来源:https://stackoverflow.com/questions/34742349/how-to-reference-a-usercontrol-from-an-external-class-library-through-a-nuget-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!