为什么 vsshell:VsBrushes 中的 DynamicResources 没有解析

Why are DynamicResources in vsshell:VsBrushes not resolved

我使用 Microsoft 此处描述的工具 window 创建了一个新的 Visual Studio 扩展 https://docs.microsoft.com/en-us/visualstudio/extensibility/creating-an-extension-with-a-tool-window?view=vs-2019

我没有对 Visual Studio 生成的代码进行任何更改,但收到两个警告:

The resource "{x:Static vsshell:VsBrushes.WindowKey}" could not be resolved.    
The resource "{x:Static vsshell:VsBrushes.WindowTextKey}" could not be resolved.    

由于以下屏幕截图中显示的两条线。

这里有一个问题非常相似

VS2010 to VS2012 ToolWindow XAML Reference VsBrushes

除了那个问题引用了 Visual Studio 2012 年。我使用的是 Visual Studio 2019 年,所以在此期间可能会有一些变化。

这个问题的答案不适合我。

谁能解释一下这种行为?自动生成的代码应该包含这些警告似乎很奇怪。

Why the warning occurs:

参见 Difference between DynamicResource and StaticResource。工具 Window 项使用 DynamicResources,因此可以在运行时设置和更改资源。

而WindowsKey和WindowsTextKey来自VsBrushes class,这两个颜色资源无法在designer-time中访问,而是可以在VsBrushes class中访问运行。这就是为什么工具 Window 控件在安装 .vsix 后运行良好而 vs 显示两个警告的原因。

另外:

如果我们将DynamicResource 更改为StaticResource,则警告将变为错误。模板中的默认值vsshell:VsBrushes.WindowKey只能在运行时访问,所以开发团队默认设置了DynamicResource。

我认为这是设计使然,警告实际上可以看作是一条消息:Possible notFoundResource, please take care! 所以我们可以忽略它。

To remove the warning:

正如我所说,这只是一条消息,告诉我们资源可能无法解析,因为设计者无法访问 "designer-time" 中的资源 vsshell:VsBrushes.WindowKey。为什么不将颜色设置为我们在设计控件时可以访问的系统颜色。

其实我们正在开发一个Window控件,当然我们可以改变控件的颜色让它变得更好。(UI-design?)所以对于我来说,我把这两个资源改成可以在 designer-time 中访问的 SystemResource 然后警告消失了:

Background="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"
Foreground="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}"