从代码设置应用程序资源

时间:2012-03-16 14:23:28

标签: c# wpf resources

我有一个作为WPF应用程序的c#项目,但我现在想将它构建为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>

这是我的代码:

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

这应该有用吗?

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

Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.

这是资源在shared.xaml

中显示的位置
<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>

这里是在toolbar.xaml

中引用的地方
<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />

对于一个问题的文章感到抱歉,但是我认为id尽可能多地提供信息。如果您还有其他需要,请告诉我。

3 个答案:

答案 0 :(得分:23)

此代码适用于我。刚刚将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);

答案 1 :(得分:6)

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

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

如果你的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) });

答案 2 :(得分:0)

您必须创建单独的ResourceDictionary文件,例如Style.xaml包含(不要忘记命名空间)

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

结束在你所有的控件中引用它