[英]Accessing a resource via codebehind in WPF
I have a custom collection defined in my window resources as follows (in a Sketchflow app so the window is actually a UserControl):我在我的 window 资源中定义了一个自定义集合,如下所示(在 Sketchflow 应用程序中,因此 window 实际上是一个 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:)
谢谢:)
You should use System.Windows.Controls.UserControl
's FindResource()
or TryFindResource()
methods.您应该使用
System.Windows.Controls.UserControl
的FindResource()
或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).此外,一个好的做法是创建一个字符串常量,它在资源字典中映射您的键的名称(以便您只能在一个地方更改它)。
You may also use this.Resources["mykey"]
.您也可以使用
this.Resources["mykey"]
。 I guess that is not much better than your own suggestion.我想这并不比你自己的建议好多少。
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"];
If you want to access a resource from some other class (ig not a xaml codebehind), you can use如果您想从其他类访问资源(不是 xaml 代码隐藏),您可以使用
Application.Current.Resources["resourceName"];
from System.Windows
namespace.来自
System.Windows
命名空间。
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); ;
}
}
我使用以下代码获得了 C#(Desktop WPF W/.NET Framework 4.8)上的资源
{DefaultNamespace}.Properties.Resources.{ResourceName}
A nice clean example from Microsoft documents makes it simple: Microsoft 文档中的一个很好的干净示例使它变得简单:
private void myButton_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
button.Background = (Brush)this.FindResource("RainbowBrush");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.