Set up application resources from code

后端 未结 3 839
情话喂你
情话喂你 2020-12-14 07:29

I have a c# project that was a WPF application but I now want to build it as a dll. I have previously done this by removing the app.xaml from the project and setting its bui

相关标签:
3条回答
  • 2020-12-14 07:45

    You have to create separate ResourceDictionary file e.g. Style.xaml that contains (don't forget namespaces)

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources/Styles/Shared.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Expander.xaml"/>
        <ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/>
        <ResourceDictionary Source="Resources/DesignerItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/>
        <ResourceDictionary Source="Resources/Connection.xaml"/>
        <ResourceDictionary Source="Resources/Slider.xaml"/>
        <ResourceDictionary Source="Resources/ScrollViewer.xaml"/>
        <ResourceDictionary Source="Resources/StatusBar.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    end reference it in all of yours controls

    0 讨论(0)
  • This code works for me. I just changed the URIs to relative:

    ResourceDictionary myResourceDictionary = new ResourceDictionary();
    
    myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
    Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
    
    myResourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.Relative);
    Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
    
    0 讨论(0)
  • 2020-12-14 07:58

    I think you need to specified the name of the component were the resource is sitting in

    <ResourceDictionary Source="/<YourDllName>;component/Resources/Styles/Shared.xaml" />
    

    If your dll is named My.Wpf.Component.dll you should put My.Wpf.Component

    so in code it should be

    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/<YourDllName>;component/Resources/Styles/Shared.xaml", UriKind.Relative) });
    
    0 讨论(0)
提交回复
热议问题