C# 通过 WPF 中的代码隐藏访问资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117886/
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
Accessing a resource via codebehind in WPF
提问by randomsequence
I have a custom collection defined in my window resources as follows (in a Sketchflow app so the window is actually a UserControl):
我在我的窗口资源中定义了一个自定义集合,如下所示(在 Sketchflow 应用程序中,因此该窗口实际上是一个 UserControl):
<UserControl.Resources>
<ds:MyCollection x:Key="myKey" x:Name="myName" />
</UserControl.Resources>
I want to be able to refer to this collection in the codebehind, which I expected would be by the x:Name, but I can't seem to access it.
我希望能够在代码隐藏中引用这个集合,我希望它是 x:Name,但我似乎无法访问它。
I can get a reference to it using
我可以使用它获得参考
myRef = (MyCollection) this.FindName("myKey");
but this seems hackish. Is this bad practice, and what would be better? Thanks :)
但这似乎是骇人听闻的。这是不好的做法,什么会更好?谢谢 :)
采纳答案by japf
You should use System.Windows.Controls.UserControl
's FindResource()
or TryFindResource()
methods.
您应该使用System.Windows.Controls.UserControl
'sFindResource()
或TryFindResource()
方法。
Also, a good practice is to create a string constant which maps the name of your key in the resource dictionary (so that you can change it at only one place).
此外,一个好的做法是创建一个字符串常量,它在资源字典中映射您的键的名称(以便您只能在一个地方更改它)。
回答by Jakob Christensen
You may also use this.Resources["mykey"]
. I guess that is not much better than your own suggestion.
您也可以使用this.Resources["mykey"]
. 我想这并不比你自己的建议好多少。
回答by itsho
Not exactly direct answer, but strongly related:
不完全是直接的答案,但密切相关:
In case the resources are in a different file - for example ResourceDictionary.xaml
如果资源位于不同的文件中 - 例如 ResourceDictionary.xaml
You can simply add x:Class
to it:
您可以简单地添加x:Class
到它:
<ResourceDictionary x:Class="Namespace.NewClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ds:MyCollection x:Key="myKey" x:Name="myName" />
</ResourceDictionary>
And then use it in code behind:
然后在后面的代码中使用它:
var res = new Namespace.NewClassName();
var col = res["myKey"];
回答by Johan Larsson
You can use a resource key like this:
您可以使用这样的资源键:
<UserControl.Resources>
<SolidColorBrush x:Key="{x:Static local:Foo.MyKey}">Blue</SolidColorBrush>
</UserControl.Resources>
<Grid Background="{StaticResource {x:Static local:Foo.MyKey}}" />
public partial class Foo : UserControl
{
public Foo()
{
InitializeComponent();
var brush = (SolidColorBrush)FindResource(MyKey);
}
public static ResourceKey MyKey { get; } = CreateResourceKey();
private static ComponentResourceKey CreateResourceKey([CallerMemberName] string caller = null)
{
return new ComponentResourceKey(typeof(Foo), caller); ;
}
}
回答by R. Matveev
If you want to access a resource from some other class (i.g. not a xaml codebehind), you can use
如果您想从其他类访问资源(不是 xaml 代码隐藏),您可以使用
Application.Current.Resources["resourceName"];
from System.Windows
namespace.
来自System.Windows
命名空间。
回答by Ranier Jardim
I got the resources on C# (Desktop WPF W/ .NET Framework 4.8) using the code below
我使用以下代码获得了 C#(Desktop WPF W/.NET Framework 4.8)上的资源
{DefaultNamespace}.Properties.Resources.{ResourceName}