Search code examples
c#wpfresourcedictionaryapplication-resource

making WPF dll, where to put Application.Resources?


I like to change Windows WPF app into .dll library. In windows App.xaml directory I have defined "Application.Resources".

<Application x:Class="WpfApplication13.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="Control" x:Key="EmptyFocusVisualStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

But because in dll library is no App.xaml file, where now to put these code? Add to my "dll-project" new "Resource Dictionary" file? But is this equivalent with "Application.Resources"?

Please for help or any example. If any your have any question, please ask.


Solution

  • In the dll project, add a resource dictionary.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style TargetType="Control" x:Key="EmptyFocusVisualStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    

    In the application that uses the dll, in the app.xaml, add the resource dictionary. In the example below, the resource dictionary is in the MyDll project with a path of MyDllSubFolder/MyResourceDictionary.xaml.

    <Application x:Class="WpfApplication13.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="/MyDll;component/MyDllSubFolder/MyResourceDictionary.xaml" />
                        </ResourceDictionary.MergedDictionaries>
                        <Style TargetType="Control" x:Key="AStyleThatIsInTheAppAndNotTheDll">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ResourceDictionary>
            </Application.Resources>
    </Application>