试图设置TextBox.IsReadOnly奇怪时,XAML解析错误 [英] Weird XAML parsing error when trying to set TextBox.IsReadOnly
问题描述
我已经成功将这一数字减少到一个简单的测试案例。一个例外是使用该XAML的分析过程中引发 XamlReader.Parse()
:
I've managed to reduce this to a simple test case. An exception is thrown during the parsing of this XAML using 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>
异常消息为:
The exception message is:
无法设置未知成员System.Windows.Controls.TextBox.IsReadOnly。行号'13'和线的位置'11'。
Cannot set unknown member 'System.Windows.Controls.TextBox.IsReadOnly'. Line number '13' and line position '11'.
如果我没有设置 IsReadOnly
在文本框
,它解析罚款。它还解析罚款,如果我删除样式触发。
If I don't set IsReadOnly
on the TextBox
, it parses fine. It also parses fine if I remove the style trigger.
任何人都可以阐明这一些轻?我相当新的WPF
Can anyone shed some light on this? I'm rather new to WPF.
更新:结果
下面是我使用重现这个单元测试(它的失败在我的电脑上):
UPDATE:
Here's the unit test I'm using to reproduce this (it's failing on my PC):
[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没有效果。
UPDATE 2:
I was originally referencing just PresentationFramework v4.0.30319. Adding references to PresentationCore, System.Xaml, and WindowsBase has no effect.
.NET项目的版本是4(全日制,而不是客户端配置文件)。
.NET version of project is 4 (full, not client profile).
更新3:结果
精氨酸,这部作品在ExpressionBlend 3.0.1927.0和XamlPadX 4.精细据报道由AresAvatar,它似乎只当 XamlReader.Parse()
或 XamlReader.Load()
!
推荐答案
简短的回答,显然这是一个错误。以下可以作为一种解决方法。
Short answer, clearly this is a bug. The following can be used as a workaround.
更新,解决办法2
哪怕只是执行下面的前行 XamlReader.Parse(XAML)
解决问题,还是无能,但为什么..
Even just executing the following line before XamlReader.Parse(xaml)
fixes the problem, still clueless as to why though..
XamlReader.Parse(@"<TextBox xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
IsReadOnly=""True""/>");
var root = XamlReader.Parse(xaml);
解决方法1 结果
在mscorlib中使用布尔代替在触发真
似乎解决好这个问题。下面的XAML不扔在 XamlReader.Parse
Workaround 1
Using Boolean in mscorlib instead of True in the Trigger
seems to fix the problem for good. The following xaml does not throw an exception in XamlReader.Parse
var xaml =
@"<DockPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:s=""clr-namespace:System;assembly=mscorlib"" >
<DockPanel.Resources>
<s:Boolean x:Key=""BooleanTrue"">True</s:Boolean>
<Style TargetType=""TextBox"">
<Style.Triggers>
<Trigger Property=""IsReadOnly"" Value=""{StaticResource BooleanTrue}"">
<Setter Property=""Background"" Value=""#FFEEEEEE"" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly=""True"" />
</DockPanel>";
的一些研究细节..
我做了这个奇怪的问题进行一些测试。
I did some testing of this weird problem.
首先,我包括在工作 DockPanel中
在XAML和
First I included the working DockPanel
in Xaml and saved it with
string xaml = XamlWriter.Save(theDockPanel);
只是为了看看那块XAML的是用 XamlReader.Parse <工作/ code>,它做到了。
just to see if that piece of xaml was working with XamlReader.Parse
, and it did.
然后我做了小的修改产生的XAML(并恢复一次例外回来了),直到我得到尽可能接近可能为原始。怪异的是,一旦这一XAML被解析,原来的作品也是如此。
Then I made small changes to the generated xaml (and reverted once the exception came back) until I got as close as possible to the original. The weird part is that once this xaml has been parsed, the original works as well.
这使它工作的部分好像是用< ; S:布尔>真< / S:布尔>
而不是真
。
The part that made it working seems to be using <s:Boolean>True</s:Boolean>
instead of True
.
var modifiedXaml = @"<DockPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:s=""clr-namespace:System;assembly=mscorlib""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<DockPanel.Resources>
<s:Boolean x:Key=""BooleanTrue"">True</s:Boolean>
<Style TargetType=""TextBox"">
<Style.Triggers>
<Trigger Property=""IsReadOnly"" Value=""{StaticResource BooleanTrue}"">
<Setter Property=""Background"" Value=""#FFEEEEEE"" />
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<TextBox IsReadOnly=""True"" />
</DockPanel>";
var originalXaml = @"<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=""{Binding}""/>
</DockPanel>";
try
{
// If this line is executed, no `XamlParseException` is thrown
var root = XamlReader.Parse(modifiedXaml);
var root2 = XamlReader.Parse(originalXaml);
}
catch (XamlParseException ex)
{
}
如果我发现这个更多的东西,我会再次更新。
I'll update again if I find something more on this..
这篇关于试图设置TextBox.IsReadOnly奇怪时,XAML解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!