我设法将其简化为一个简单的测试用例。在使用以下 XAML 解析期间引发异常XamlReader.Parse()
:
<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="#FFEEEEEE" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly="True" />
</DockPanel>
异常消息是:
无法设置未知成员“System.Windows.Controls.TextBox.IsReadOnly”。行号“13”和行位置“11”。
如果我没有设置IsReadOnly
,TextBox
它解析得很好。如果我删除样式触发器,它也可以很好地解析。
任何人都可以对此有所了解吗?我对 WPF 比较陌生。
更新:
这是我用来重现这个的单元测试(它在我的电脑上失败了):
[TestMethod]
public void TestIsReadOnlyOnTextBox()
{
// Arrange
var xaml =
@"<DockPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<DockPanel.Resources>
<Style TargetType=""TextBox"">
<Style.Triggers>
<Trigger Property=""IsReadOnly"" Value=""True"">
<Setter Property=""Background"" Value=""#FFEEEEEE"" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly=""True"" />
</DockPanel>
";
// Act
try {
var root = XamlReader.Parse(xaml);
}
catch (XamlParseException ex) {
Assert.Fail(ex.Message);
}
// If we get here, test passes
}
更新 2:
我最初只是引用 PresentationFramework v4.0.30319。添加对 PresentationCore、System.Xaml 和 WindowsBase 的引用无效。
.NET 版本的项目是 4(完整,不是客户端配置文件)。
更新 3:
Arg,这在 ExpressionBlend 3.0.1927.0 和 XamlPadX 4 中运行良好。正如 AresAvatar 报告的那样,它似乎只在使用XamlReader.Parse()
or解析时才会失败XamlReader.Load()
!