5

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

Tommaso Scalici
  • 480
  • 1
  • 6
  • 15
  • Post XAML, where `UserControl1` used. – Dennis Jan 12 '16 at 11:26
  • 1
    did you specify the assembly in the namespace like this? http://stackoverflow.com/a/3297719/1328536 – fuchs777 Jan 12 '16 at 11:30
  • @fuchs777 Tried also specifying the namespace in that way, XAML parsing still failing – Tommaso Scalici Jan 12 '16 at 11:43
  • Did you add a reference to the project `ClassLibrary1` in your other project (in which this page exists) ? – Tom Wuyts Jan 12 '16 at 11:51
  • @TomWuyts yes, I have just App1 project referencing ClassLibrary1. This was a test to see if I can reference custom controls from external libraries, but seems this can't be done or that I'm missing something – Tommaso Scalici Jan 12 '16 at 12:06
  • Interesting when I tried your code with my VS2015, it successfully builds and runs. – Jackie Jan 12 '16 at 12:57
  • One other (obscure) thing I could think off: check the build action of your usercontrol's xaml. It should be set `Page`, `Copy to Output Dir` can be `Do Not Copy`, and make sure the `Custom Tool` is set to `MSBuild:Compile`. – Tom Wuyts Jan 12 '16 at 13:28
  • @Jackie added details to the question – Tommaso Scalici Jan 12 '16 at 14:17

2 Answers2

2

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!

Tommaso Scalici
  • 480
  • 1
  • 6
  • 15
  • 1
    Thanks for your insights. I was actually facing this problem even in 2020 but there seems to be no good tutorial on this. Based on your input, I have written a detailed steps to help myself and future others who have similar issues: https://cuteprogramming.wordpress.com/2020/04/26/pack-uwp-class-library-with-nuget/ – Chun Lin Apr 26 '20 at 08:27
1

Your code works perfectly. Try to create project anew.

Ivan Zinovyev
  • 146
  • 1
  • 4
  • Yes, it worked perfectly using a standard reference, I forgot to specify that what I'm trying to achieve is a reference to a library (with custom xaml controls) using a NuGet Package. In that way the exception come up. I honestly didn't thought this would be an important detail neither the source of the issue :P I added details to the question – Tommaso Scalici Jan 12 '16 at 13:41