Unable to create UserControl instance from another assembly. Resource is not found.

I have a solution with two WPF projects: Primary and Secondary.

In the main project, a window called PrimaryView creates an instance of UserControl called SecondaryControl, defined in the Secondary project.

SecondaryControl uses the SecondaryStyle, which is defined in SecondaryResourceDictionary (as you might guess, defined in SecondaryProject).

The thing is, when I try to run the solution, I get a XamlParseError and dig up InnerExceptions. In the end, I find the culprit, ResourceNotFound error.

So my questions are:

  • If SecondaryControl and its SecondaryStyle are defined in the same assembly, why can't I instantiate PrimaryAssembly?

  • Should I make SecondaryStyle accessible to the PrimaryProject namespace in some way? Why?

0
wpf resourcedictionary xamlparseexception
source share
1 answer

I am trying to help you explain how this works in my projects. A separate assembly contains a common vocabulary of control and shared resource similar to this:

CommonResources.xaml

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!-- Some resources --> </ResourceDictionary> 

SomeCommonControl.xaml

 <UserControl x:Class="YourAssembly.SomeCommonControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <!-- Some specific content --> </UserControl> 

I can use this control and resources from other WPF builds and projects, for example:

 <Window x:Class="YourWPFProject.SomeWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:common="clr-namespace:YourAssembly"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/CommonResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <common:SomeCommonControl /> </Window> 

Hope this helps you.

+1
source share

All Articles