问题
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