I have a class library project which I am using in a UWP project. If I add any usercontrol xaml file in the class library and build it it builds fine. But the UWP project gives an error that it cannot find the .xr file.
Do I need to add the .xr file externally in any folder to my UWP project?
Declaring x:Class on any element other than a root node, and under any circumstances for a XAML file that is not compiled with the Page build action, results in a compile-time error. The class used as x:Class cannot be a nested class.
Turning a UWP-only class library into an Uno cross-platform class library entails changing the contents of the existing .csproj file. The easiest way to do that is to create a temporary Uno class library, and copy its contents into the existing .csproj file. The steps in detail:
Whether defined in the same or different assembly, custom classes need to be mapped between CLR namespace and XML namespace in order to be used in XAML as elements. See XAML Namespaces and Namespace Mapping for WPF XAML. In order to be able to be instantiated as an object element, your class must meet the following requirements:
XAML as implemented in common language runtime (CLR) frameworks supports the ability to define a custom class or structure in any common language runtime (CLR) language, and then access that class using XAML markup.
Yeah, for a class library with XAML files, if we want to reference the dll in other project, we need not only the dll itself but also the .xr.xaml file and some other files. Because in UWP environment, the resources are no longer embedded in the assembly but are placed next to the dll as content. See the similar case: How to add xbf files to visual studio project.
The files we need to reference like following:
- ClassLibrary1(Class Library name) Folder
- ClassLibrary1.xr.xml
- UserControl.xaml (UserControl XAML file)
- ClassLibrary1.dll
- ClassLibrary1.pri
To get these files, we can check the "Generate library layout" option in the Build configuration under the project's Properties page.
Then we can copy these files to anywhere and the UWP project just need to add reference to the ClassLibrary1.dll file in the Visual Studio, Visual Studio will automatically pick these files up and put them in the appx package when it builds the app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With