0

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

In the Primary project, a Window named PrimaryView instantiates an UserControl called SecondaryControl, defined in Secondary project.

SecondaryControl uses SecondaryStyle, which is defined in SecondaryResourceDictionary (as you might have guessed already, defined in SecondaryProject).

The fact is: when I try to run the solution, I get a XamlParseError, and digging InnerExceptions I eventually find the culprit, the ResourceNotFound error.

So my questions are:

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

  • Should I make SecondaryStyle available to PrimaryProject namespace somehow? Why?

Community
  • 1
  • 1
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • In this case I usually keep common controls and common resources in a separate assembly and it works fine. Have you tried to do the same? – hcp Oct 05 '15 at 14:14
  • @hypercodeplace from what I understood of your explanation, that is already my situation. `SecondaryProject` is the separate assembly you talk about, containing a common control (`SecondaryControl`) and a common resource (`SecondaryStyle`). – heltonbiker Oct 05 '15 at 14:21
  • Do you use implicit style or explicit style? – user2250152 Oct 05 '15 at 14:33
  • @user2250152 I use explicit style. `SecondaryControl` contains a `Thumb` which is stiled with `SecondaryStyle`, whose TargetType is Thumb. Making an analogy, it would be like an "internal" Style, that is, a Style intended to be use internally by the assembly containing the UserControls. Or putting it another way, in PrimaryAssembly I'll never apply styles define SecondaryAssembly, but instead use Controls which in turn are styled with styles from SecondaryAssembly. – heltonbiker Oct 05 '15 at 14:38
  • @heltonbiker I am having a similar issue. I have a UserControl in my library - a popup, which when my app calls from the library, it gives a xamlparseexception. the popup is something like the one here - http://stackoverflow.com/questions/19032750/how-to-center-a-popup-in-window-windows-store-apps, can you explain how you managed to overcome this problem? – kshitijgandhi Dec 08 '15 at 06:58

1 Answers1

1

I try to help you by explanation how it works in my projects. A separate assembly contains a common control and common resource dictionary like 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 another assemblies and WPF-projects like this:

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

I hope this will help you.

hcp
  • 3,268
  • 6
  • 26
  • 41
  • Thanks for your answer, I have already solved the problem, and your solution is right. Only one detail: if you are using .NET 4.5 (as it is my case), you must remove part or the URI syntax, and make it like this: `` . Only then things worked fine. – heltonbiker Oct 06 '15 at 13:44