从代码设置应用程序资源

use*_*195 23 c# wpf resources

我有一个作为WPF应用程序的ac #project,但我现在想把它构建为一个dll.我以前通过从项目中删除app.xaml并将其构建类型设置为dll来完成此操作.

我现在的问题是app.xaml包含一些xaml来实例化应用程序变量.为了解决这个问题,我试图以编程方式在将要调用的第一个xaml窗口中设置这些应用程序变量.

我试图在代码中模拟的xaml是:

<Application.Resources>
    <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>
  </Application.Resources>
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

ResourceDictionary myResourceDictionary = new ResourceDictionary();
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Shared.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\GroupBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ZoomBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ScrollBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Expander.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\ApplicationToolbar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\DesignerItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolboxItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Toolbox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Connection.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Slider.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\ScrollViewer.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\StatusBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
Run Code Online (Sandbox Code Playgroud)

这有用吗?

我遇到了一个问题,因为Toolbar.xaml引用了在Shared.xaml中声明的资源但它没有被拾取并且我得到以下错误.

Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.
Run Code Online (Sandbox Code Playgroud)

这是资源在shared.xaml中进行delcared的地方

<LinearGradientBrush x:Key="ToolbarSelectedBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#FFFEE3" Offset="0.0"/>
        <GradientStop Color="#FFE797" Offset="0.4"/>
        <GradientStop Color="#FFD750" Offset="0.4"/>
        <GradientStop Color="#FFE796" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>
Run Code Online (Sandbox Code Playgroud)

这是在toolbar.xaml中引用的地方

<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />
Run Code Online (Sandbox Code Playgroud)

对于一个问题的文章感到抱歉,但思想ID提供尽可能多的信息.需要帮助请叫我.

Nes*_*uro 25

这段代码适合我.刚刚将URIS改为亲戚:

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);
Run Code Online (Sandbox Code Playgroud)


Gui*_*ume 7

我认为您需要指定资源所在的组件的名称

<ResourceDictionary Source="/<YourDllName>;component/Resources/Styles/Shared.xaml" />
Run Code Online (Sandbox Code Playgroud)

如果你的dll名为My.Wpf.Component.dll,你应该放置My.Wpf.Component

所以在代码中它应该是

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/<YourDllName>;component/Resources/Styles/Shared.xaml", UriKind.Relative) });
Run Code Online (Sandbox Code Playgroud)