XAML 语法错误阻止设计器但编译正常 [英] XAML syntax error is blocking designer but compiling fine

查看:20
本文介绍了XAML 语法错误阻止设计器但编译正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一件相当奇怪的事情,我在 WPF XMAL 页面中定义了一个 DataGrid,并声明了以下内容:

I've run into a rather bizarre thing happening, I have a DataGrid defined in a WPF XMAL page that has the following declared:

<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>

从技术上讲,这两种 Inactive SystemColors 类型来自 .net 4.5,但是当它设置为目标 .net 4 时我可以编译程序,并且这些 Inactive 画笔工作,但是当它设置为目标 .net 4 在 Visual Studio 中加载 XMAL 设计器页面时会引发错误成员InactiveSelectionHighlightTextBrushKey"无法识别或不可访问.然后阻止设计器视图.但它仍然按照上面程序中的定义进行编译和显示.

Technically the two Inactive SystemColors types 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.

至少可以说这似乎非常不一致,我不知道这是 Visual Studio 2012 问题还是它允许程序编译,因为我的开发计算机安装了 .net 4.5 并且它只是在更改目标框架时它看到有什么东西在使用它(虽然我非常怀疑).或者是否有可能 Inactive 类型在 .net 4 中但未在文档中列为受支持,而只是在 VS 中导致此问题?

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?

在 .net 4 中是否有更好的方法可以让我设置 DataGrid 行的非活动选择颜色?或者升级到 .net 4.5 是唯一的方法吗?

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?

推荐答案

Visual Studio 生成程序集,即使其目标设置为 .NET FW 4.0 并且您在 XAML 代码中使用 InactiveSelectionHighlightBrushKey.此程序集将在具有 .NET FW 4.5 的系统中正确执行.但是如果系统只有.NET FW 4.0,当系统使用InactiveSelectionHighlightBrushKey创建用户控件时会抛出异常.

Visual Studio builds an assembly, even if its target set to .NET FW 4.0 and you use InactiveSelectionHighlightBrushKey in 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 thrown when the system create User control with InactiveSelectionHighlightBrushKey.

因此您不能在目标设置为 FW 4.0 的程序集中使用 InactiveSelectionHighlightBrushKey,因为它们在只有 .NET FW 4.0 的系统中不起作用.

So you cannot use the InactiveSelectionHighlightBrushKey in assemblies with target set to FW 4.0, because they will not work in a system with only .NET FW 4.0.

要同时支持 FW 4.0 和 FW 4.5,您可以在 LostFocus/LostKeyboardFocus/GotFocus 事件的处理程序中设置所选行的颜色.查看示例代码 https://stackoverflow.com/a/8095932/1815957

To support both FW 4.0 and FW 4.5 you can set a color of the selected row in handlers of LostFocus/LostKeyboardFocus/GotFocus events. See the example code https://stackoverflow.com/a/8095932/1815957

这篇关于XAML 语法错误阻止设计器但编译正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆