locked
WPF Resource Dictionary with code behind file can’t be found by using a separated assembly RRS feed

  • Question

  • Hello,

    I have the assembly "WpfStyle.dll" where I put all my WPF styles for my main app and It works as expected, but as soon as I add a code behind file to the XAML of the ResourceDictionary, the following error is thrown: "An error occurred while finding the resource dictionary".

    Note: I get the issue only when I add the "WpfStyle.dll" as assembly reference. If I add it as project reference all works fine.

    Here below, some others info about the "WpfStyle.dll" structure:

    1) "WindowStyle.xaml" and "WindowStyle.xaml.cs" files are placed in "Themes" subfolder

    2) Here below a piece of code of the ResourceDictionary file ("WindowStyle.xaml"):

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        x:Class="WpfStyle.Themes.WindowStyle">
    
    ...
    
    </ResourceDictionary>


    3) Here below a piece of code behind ("WindowStyle.xaml.cs"):

    namespace WpfStyle.Themes
    {
      public partial class WindowStyle : ResourceDictionary
      {
        public WindowStyle()
        {
          InitializeComponent();
      
        }
    
       ...
    
      }
    }


    4) Here below the App.xaml of the main app that throws the error:

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>                
                    <ResourceDictionary Source="pack://application:,,,/WpfStyle;component/Themes/WindowStyle.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

    What am I doing wrong?




    • Edited by ilCosmico Monday, March 30, 2015 9:42 AM
    Monday, March 30, 2015 9:25 AM

Answers

  • Resource dictionaries don't usually have code behind.

    You could encapsulate behaviour as attached behaviors which set attached properties.

    Then do stuff based on those properties changing which you apply via a style.

    That's the generic approach.

    That's all window orientated stuff I see there.

    You can also define a partial class as a base window which all your windows inherit from.

    By base class I mean code only - you can't inherit xaml.

    You inherit:

    <local:BaseWindow
      x:Class="wpf1.MainWindow"
       ....
      xmlns:local="clr-namespace:wpf1"
    Where your app namespace would be substituted for wpf1.

    Google it for more info, it's a standard (though a bit counter intuitive in some ways ) approach.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML
    Practical Polly


    • Edited by Andy ONeill Tuesday, March 31, 2015 11:17 AM
    • Marked as answer by ilCosmico Thursday, April 2, 2015 11:43 AM
    Tuesday, March 31, 2015 11:16 AM

All replies

  • If you add the reference by browsing to the compiled .dll file of the ResourceDictionary you must make sure that you manually build this .dll each time you make a change to it before you run the WPF application.

    Visual Studio won't build it automatically for you when you haven't added a reference to the actual project in the solution.

    Right-click on the WpfStyle project in the Solution Explorer in VS and choose "Build". This will create a new (and overwrite the existing one) .dll and if you reference this one from the WPF application you should then be able to run it and use any style that is defined in WpfStyle.dll.

    If you need more help on this and want someone to be able to point of what you are doing wrong, then please upload a reproducable sample of your issue to OneDrive and post the link to it here.


    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please only ask one question per thread.

    Monday, March 30, 2015 1:44 PM
  • Hello,

    Thank you for your reply.

    Unfortunately, if I try to insert a link I get the following message: "Body text cannot contain images or links until we are able to verify your account." If you give me your email address I can send you a sample code.

    In the meantime I made some others tests and I found that my problem seems to be linked to some Images that I used in my Resource Dictionary.

    Here below my sample updated:

    WindowStyle.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    
                        x:Class="WpfStyle.Themes.WindowStyle">
    
        <BitmapImage x:Key="ImageNormalClose" UriSource="Images/normalClose.png"></BitmapImage>
    
        <Style x:Key="Test" TargetType="Image">
            <Setter Property="Source" Value="{StaticResource ImageNormalClose}"/>
        </Style>
    
    
    </ResourceDictionary>



    • Edited by ilCosmico Tuesday, March 31, 2015 8:51 AM
    Tuesday, March 31, 2015 8:50 AM
  • Why have you got any code behind for your resource dictionary?

    I think putting your resources in a separate dll like this is a bad idea, btw.

    PARTICULALRLY if you're referring to images though.

    Also.

    Have you considered using paths rather than images?

    That's what I usually do.

    They have a number of advantages:

    • Way more efficient
    • Reside purely in xaml in your resource dictionary ( no relative path issues ).
    • Win 8 friendly

    EG the phones in this https://gallery.technet.microsoft.com/WPF-Dialler-simulator-d782db17

    And all  the button "icons" in this:

    http://social.technet.microsoft.com/wiki/contents/articles/28209.wpf-entity-framework-mvvm-walk-through-1.aspx


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML
    Practical Polly



    • Edited by Andy ONeill Tuesday, March 31, 2015 9:00 AM
    Tuesday, March 31, 2015 8:58 AM
  • Hello Andy,

    In this way the issue about the images is solved, thank you.

    Anyway I still get the error because of code-behind file.

    I'm trying to build a complete Windows style, so I need of the code-behind file to manage some events (eg. MoveWindow, CloseWindow, MinimizeWindow, WindowSizeChanged and so on).

    I think putting your resources in a separate dll like this is a bad idea.

    I want to make a separate dll because I need to use the same windows style in many differents projects

    Any other useful suggestion for me?


    • Edited by ilCosmico Tuesday, March 31, 2015 11:09 AM
    Tuesday, March 31, 2015 11:06 AM
  • Resource dictionaries don't usually have code behind.

    You could encapsulate behaviour as attached behaviors which set attached properties.

    Then do stuff based on those properties changing which you apply via a style.

    That's the generic approach.

    That's all window orientated stuff I see there.

    You can also define a partial class as a base window which all your windows inherit from.

    By base class I mean code only - you can't inherit xaml.

    You inherit:

    <local:BaseWindow
      x:Class="wpf1.MainWindow"
       ....
      xmlns:local="clr-namespace:wpf1"
    Where your app namespace would be substituted for wpf1.

    Google it for more info, it's a standard (though a bit counter intuitive in some ways ) approach.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML
    Practical Polly


    • Edited by Andy ONeill Tuesday, March 31, 2015 11:17 AM
    • Marked as answer by ilCosmico Thursday, April 2, 2015 11:43 AM
    Tuesday, March 31, 2015 11:16 AM
  • Hello Andy,

    I found this page in which is explained the goal that I was trying to reach

    (You need to add "." after "www")

    wwwequals-forty-two.com/2010/11/22/wpf-custom-window-styles

    Here below the link to download my test app after your previous suggestion:

    wwwwetransfer.com/downloads/281246b7ea64f31ac62c70b5308e929e20150331122434/39c910

    Finally, here you can see the error:

    screencast.com/t/V86DrGZsE

    About your last post, is your approach something like that one shown in the below link?

    blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/





    • Edited by ilCosmico Tuesday, March 31, 2015 3:02 PM
    Tuesday, March 31, 2015 12:29 PM
  • Thanks for your answer . It works for me
    Sunday, July 15, 2018 12:25 PM