wpf XAML 语法错误阻止设计器但编译正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13810772/
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
XAML syntax error is blocking designer but compiling fine
提问by siva.k
I've run into a rather bizarre thing happening, I have a DataGriddefined in a WPF XMAL page that has the following declared:
我遇到了一件相当奇怪的事情,我DataGrid在 WPF XMAL 页面中定义了一个声明如下:
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF3399FF" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF3399FF"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
</DataGrid.Resources>
Technically the two InactiveSystemColorstypes are from .net 4.5, however I can compile the program when it's set to target .net 4, and these Inactive brushes work, but while it's set to target .net 4 loading the XMAL designer page in Visual Studio throws the error The member "InactiveSelectionHighlightTextBrushKey" is not recognized or is not accessible.and then blocks the designer view. But it still compiles and displays as defined above in the program.
从技术上讲,这两种InactiveSystemColors类型来自 .net 4.5,但是当它设置为目标 .net 4 时我可以编译程序,并且这些非活动画笔工作,但是当它设置为目标 .net 4 时,在 Visual Studio 中加载 XMAL 设计器页面会抛出错误The member "InactiveSelectionHighlightTextBrushKey" is not recognized or is not accessible.,然后阻止设计器视图。但它仍然按照上面程序中的定义进行编译和显示。
This seems very inconsistent to say the least and I can't tell if this is a Visual Studio 2012 issue or if it's allowing the program to compile because my development computer has .net 4.5 installed and it's just changing the target framework when it sees that something is using it (I highly doubt this though). Or is it possible that the Inactive types are in .net 4 but not listed as supported in the documentation and just causing this issue in VS?
至少可以说这似乎非常不一致,我不知道这是 Visual Studio 2012 问题还是它允许程序编译,因为我的开发计算机安装了 .net 4.5 并且它只是在看到时更改目标框架有些东西正在使用它(虽然我非常怀疑这一点)。或者 Inactive 类型是否可能在 .net 4 中但未在文档中列为受支持,而只是在 VS 中导致此问题?
Is there a better way to do this in .net 4 to allow me to set the inactive selection color of a DataGrid row? Or is the only way to do this being to upgrade to .net 4.5?
在 .net 4 中是否有更好的方法可以让我设置 DataGrid 行的非活动选择颜色?或者是升级到 .net 4.5 的唯一方法?
回答by Mikhail Shcherbakov
Visual Studio builds an assembly, even if its target set to .NET FW 4.0 and you use InactiveSelectionHighlightBrushKeyin XAML-code. This assembly will be correctly performed in a system with .NET FW 4.5. But if the system has only .NET FW 4.0, an exception will be thrownwhen the system create User control with InactiveSelectionHighlightBrushKey.
Visual Studio 生成程序集,即使其目标设置为 .NET FW 4.0 并且您InactiveSelectionHighlightBrushKey在 XAML 代码中使用。此程序集将在具有 .NET FW 4.5 的系统中正确执行。但是如果系统只有.NET FW 4.0,当系统使用.NET FW 4.0创建用户控件时会抛出异常InactiveSelectionHighlightBrushKey。
So you cannot use the InactiveSelectionHighlightBrushKeyin assemblies with target set to FW 4.0, because they will not work in a system with only .NET FW 4.0.
所以你不能使用InactiveSelectionHighlightBrushKey目标设置为 FW 4.0 的程序集,因为它们在只有 .NET FW 4.0 的系统中不起作用。
To support both FW 4.0 and FW 4.5 you can set a color of the selected row in handlers of LostFocus/LostKeyboardFocus/GotFocusevents. See the example code https://stackoverflow.com/a/8095932/1815957
要同时支持 FW 4.0 和 FW 4.5,您可以在LostFocus/LostKeyboardFocus/GotFocus事件处理程序中设置所选行的颜色。查看示例代码https://stackoverflow.com/a/8095932/1815957
回答by David
If you don't want to use code behind then an alternative to InactiveSelectionHighlightBrushKeyis ControlBrushKey. The following worked for me:
如果你不希望使用的代码背后则替代InactiveSelectionHighlightBrushKey的ControlBrushKey。以下对我有用:
<Style x:Key="ReadOnlyDataGrid" TargetType="{x:Type DataGrid}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGreen"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/>
</Style.Resources>
</Style>

