Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中使用StaticResource和x:Static_C#_Wpf_Code Behind_Markup Extensions - Fatal编程技术网

C# 在代码隐藏中使用StaticResource和x:Static

C# 在代码隐藏中使用StaticResource和x:Static,c#,wpf,code-behind,markup-extensions,C#,Wpf,Code Behind,Markup Extensions,我喜欢: 将抛出: WindowsBase.dll中发生“System.Windows.ResourceReferenceKeyNotFoundException”类型的异常,但未在用户代码中处理 其他信息:未找到“ToolBar.ButtonStyleKey”资源 根据您的工作代码,它应该如下所示: Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey) 换句话说,抛开引用。ButtonStyleKey不

我喜欢:

将抛出:

WindowsBase.dll中发生“System.Windows.ResourceReferenceKeyNotFoundException”类型的异常,但未在用户代码中处理

其他信息:未找到“ToolBar.ButtonStyleKey”资源


根据您的工作代码,它应该如下所示:

Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey)
换句话说,抛开引用。ButtonStyleKey不是名称,它是返回具有正确名称的字符串的静态属性

var button = new Button
{
    Style = (Style)Application.Current.FindResource("ToolBar.ButtonStyleKey"), // wrong
    BorderThickness = new Thickness(0),
    ...
};
Style = (Style)Application.Current.FindResource(ToolBar.ButtonStyleKey)