从代码中设置应用程序资源

28
我有一个 C# 项目,原本是一个 WPF 应用程序,但现在我想将它构建为 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}" />

抱歉我的问题有点啰嗦,但我想提供尽可能多的信息。如果你需要其他帮助,请告诉我。


你的问题涉及编写代码,因为你不再使用 App.xaml 并想要编写代码来解决问题。事实证明,你仍然可以使用 App.xaml,参见 https://dev59.com/ilLTa4cB1Zd3GeqPcada#4441500 。然后调用 DLL 实例化 App,就像 https://dev59.com/6XE85IYBdhLWcg3wnUwd#26890426 中所示。对我来说,所有资源都能正常工作,设计时和运行时都能正常使用相对 URI。 - Stéphane Gourichon
3个回答

34

这段代码对我有效。我只是将URI更改为相对路径:

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

12
我认为你需要指定资源所在的组件名称。
<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) });

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>

在所有的控件中引用它


他可以从代码创建这样的标记,并将其附加到可视树中。 - pamidur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接