mat*_*i82 3 wpf resources xaml
标题中的问题几乎总结了一下.我在XAML中定义了一些资源对象,我也想在后面的代码中访问它.那么有没有办法在一个地方定义x:Key而不是硬编码x:XAML和后面的代码中的Key(作为字符串)?
如果你不想对字符串进行两次编码,你可以将它存储为静态变量,这里我把它放在App.cs中
public partial class App : Application
{
public static string Key1 = "testKey";
}
Run Code Online (Sandbox Code Playgroud)
当您想在应用程序的资源中使用此密钥时,您可以这样做.
<Application.Resources>
<SolidColorBrush x:Key="{x:Static local:App.Key1}"/>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
在C#中,您不再需要使用确切的字符串名称,因为它位于App中
var brush = FindResource(App.Key1);
Run Code Online (Sandbox Code Playgroud)
要在XAML中使用资源,请使用
<TextBox Background="{StaticResource {x:Static local:App.Key1}}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3104 次 |
| 最近记录: |