对没有默认构造函数的控件使用 XamlReader
我有一些 Xaml 对象的字符串表示形式,并且我想构建控件。我正在使用 XamlReader.Parse 函数来执行此操作。对于像 Button 这样的简单控件,它具有不带任何参数的默认构造函数,这可以正常工作:
var buttonStr = "<Button xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">Text</Button>";
var button = (Button)XamlReader.Parse(buttonStr);
但是,当我尝试对 Stroke 控件执行此操作时,它会失败。首先尝试一个简单的空笔画:
var strokeStr = "<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"></Stroke>";
var stroke = (Stroke)XamlReader.Parse(strokeStr);
这给出了错误:
无法创建“System.Windows.Ink.Stroke”类型的对象。 CreateInstance 失败,这可能是由于没有“System.Windows.Ink.Stroke”的公共默认构造函数造成的。
在 Stroke 的定义中,我发现它至少需要构造一个 StylusPointsCollection。我认为这就是错误告诉我的内容,尽管有点假设这将由 XamlReader 处理。尝试使用 StylusPoints 转换 Stroke 的 Xaml 会出现相同的错误:
var strokeStr =
"<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
"<Stroke.StylusPoints>" +
"<StylusPoint X=\"100\" Y=\"100\" />" +
"<StylusPoint X=\"200\" Y=\"200\" />" +
"</Stroke.StylusPoints>" +
"</Stroke>";
var stroke = (Stroke) XamlReader.Parse(strokeStr);
我做错了什么?如何告诉 XamlReader 如何正确创建 Stroke?
I have some string representations of Xaml objects, and I want to build the controls. I'm using the XamlReader.Parse function to do this. For a simple control such as Button that has a default constructor not taking any parameters this works fine:
var buttonStr = "<Button xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">Text</Button>";
var button = (Button)XamlReader.Parse(buttonStr);
However, when I try to do this to e.g. a Stroke control it fails. First trying a simple empty Stroke:
var strokeStr = "<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"></Stroke>";
var stroke = (Stroke)XamlReader.Parse(strokeStr);
This gives the error:
Cannot create object of type 'System.Windows.Ink.Stroke'. CreateInstance failed, which can be caused by not having a public default constructor for 'System.Windows.Ink.Stroke'.
In the definition of Stroke I see that it needs at least a StylusPointsCollection to be constructed. I assume this is what the error is telling me, though was kinda assuming this would be handled by the XamlReader. Trying to transform a Xaml of Stroke with StylusPoints in it gives the same error:
var strokeStr =
"<Stroke xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
"<Stroke.StylusPoints>" +
"<StylusPoint X=\"100\" Y=\"100\" />" +
"<StylusPoint X=\"200\" Y=\"200\" />" +
"</Stroke.StylusPoints>" +
"</Stroke>";
var stroke = (Stroke) XamlReader.Parse(strokeStr);
What am I doing wrong? How do I tell the XamlReader how to create the Stroke correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是 XAML 语言的一个“功能”,它是声明性的,并且不了解有关构造函数的任何信息。
人们在 XAML 中使用 ObjectDataProvider 来“翻译” ”并包装没有无参数构造函数的类的实例(它对于数据绑定也很有用< /a>)。
在您的情况下,XAML 应大致如下所示:
代码应为:
HTH。
It's a "feature" of the XAML language, it is declarative and doesn't know anything about constructors.
People use ObjectDataProvider in XAML to "translate" and wrap instances of classes that do not have a parameterless constructor (it's also useful for data binding).
In your case the XAML should look approximately like this:
And the code should be:
HTH.