C# 从代码设置应用程序资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9739032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set up application resources from code
提问by user589195
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 build type to dll.
我有一个 ac# 项目,它是一个 WPF 应用程序,但我现在想将它构建为一个 dll。我以前通过从项目中删除 app.xaml 并将其构建类型设置为 dll 来完成此操作。
The issue I have now is that the app.xaml contains some xaml to instantiate the application variables. To get round this I am trying to programmatically set these application variables from within the first xaml window that will be called.
我现在的问题是 app.xaml 包含一些 xaml 来实例化应用程序变量。为了解决这个问题,我试图从将被调用的第一个 xaml 窗口中以编程方式设置这些应用程序变量。
The xaml I am trying to emulate in code is:
我试图在代码中模拟的 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>
This is the code I have:
这是我的代码:
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);
Should this work?
这应该工作吗?
I'm hitting a problem in that Toolbar.xaml references a resource declared in Shared.xaml but its not getting picked up and im getting the following error.
我遇到了一个问题,即 Toolbar.xaml 引用了在 Shared.xaml 中声明的资源,但它没有被选中,我收到以下错误。
Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.
Here is where the resource is delcared in shared.xaml
这里是资源在 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>
and here's where its referenced in toolbar.xaml
这是在toolbar.xaml中引用的地方
<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />
Sorry for the essay of a question but thought id provide as much info as I could. Let me know if you need anything else.
对于问题的文章感到抱歉,但我认为 id 提供了尽可能多的信息。需要帮助请叫我。
采纳答案by NestorArturo
This code works for me. I just changed the URIs to relative:
这段代码对我有用。我只是将 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);
回答by pamidur
You have to create separate ResourceDictionary file e.g. Style.xaml that contains (don't forget namespaces)
您必须创建单独的 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>
end reference it in all of yours controls
在你所有的控件中引用它
回答by Guillaume
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
如果你的 dll 被命名为 My.Wpf.Component.dll 你应该把 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) });

