gpt4 book ai didi

c# - 尝试设置 TextBox.IsReadOnly 时出现奇怪的 XAML 解析错误

转载 作者:IT王子 更新时间:2023-10-29 04:31:30 34 4
gpt4 key购买 nike

我已经设法将其简化为一个简单的测试用例。在使用 XamlReader.Parse() 解析此 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>

异常信息是:

Cannot set unknown member 'System.Windows.Controls.TextBox.IsReadOnly'. Line number '13' and line position '11'.

如果我没有在 TextBox 上设置 IsReadOnly,它可以正常解析。如果我删除样式触发器,它也可以正常解析。

任何人都可以阐明这一点吗?我对 WPF 比较陌生。

更新:
这是我用来重现此内容的单元测试(它在我的 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 的引用没有任何效果。

项目的 .NET 版本是 4(完整的,不是客户端配置文件)。

更新 3:
Arg,这在 ExpressionBlend 3.0.1927.0 和 XamlPadX 4 中运行良好。据 AresAvatar 报道,它似乎仅在使用 XamlReader.Parse()XamlReader.Load() !

最佳答案

简短的回答,显然这是一个错误。以下方法可用作解决方法。

更新,解决方法 2

即使只是在 XamlReader.Parse(xaml) 之前执行以下行解决了问题,但仍然不知道为什么..

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 中使用 Boolean 而不是在 Trigger 中使用 True似乎永远解决了这个问题。以下 xaml 不会在 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>";

一些研究细节..

我对这个奇怪的问题做了一些测试。

首先我包括了工作 DockPanel在 Xaml 中并将其保存为

string xaml = XamlWriter.Save(theDockPanel);

只是为了看看那段 xaml 是否与 XamlReader.Parse 一起工作,它做到了。

然后我对生成的 xaml 做了一些小改动(并在异常返回后恢复),直到我尽可能接近原始文件。奇怪的是,一旦这个 xaml 被解析,原来的也能工作。

使它工作的部分似乎正在使用 <s:Boolean>True</s:Boolean>而不是 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)
{

}

如果我发现更多关于此的内容,我会再次更新..

关于c# - 尝试设置 TextBox.IsReadOnly 时出现奇怪的 XAML 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850713/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com