将XamlReader用于没有默认构造函数的控件 [英] Using XamlReader for controls that does not have a default constructor

查看:65
本文介绍了将XamlReader用于没有默认构造函数的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Xaml对象的字符串表示形式,并且我想构建控件.我正在使用 XamlReader.Parse 函数来执行此操作.对于简单的控件(如Button),它具有不带任何参数的默认构造函数,则可以正常工作:

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

但是,当我尝试这样做时,例如一个Stroke控件失败.首先尝试一个简单的空行程:

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

这给出了错误:

无法创建类型为"System.Windows.Ink.Stroke"的对象.CreateInstance失败,可能是由于没有System.Windows.Ink.Stroke的公共默认构造函数而导致的.

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

在Stroke的定义中,我看到它至少需要构造一个StylusPointsCollection.我假设这就是错误告诉我的内容,尽管有点假设这将由XamlReader处理.尝试用StylusPoints转换Xaml的笔触会产生相同的错误:

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

我做错了什么?如何告诉XamlReader如何正确创建Stroke?

What am I doing wrong? How do I tell the XamlReader how to create the Stroke correctly?

推荐答案

这是XAML语言的功能",它是声明性的,对构造函数一无所知.

It's a "feature" of the XAML language, it is declarative and doesn't know anything about constructors.

人们在XAML中使用 ObjectDataProvider 来翻译"并包装没有无参数构造函数的类的实例(它的对于数据绑定).

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

在您的情况下,XAML应该看起来像这样:

In your case the XAML should look approximately like this:

<ObjectDataProvider ObjectType="Stroke">
    <ObjectDataProvider.ConstructorParameters>
        <StylusPointsCollection>
            <StylusPoint X="100" Y="100"/>
            <StylusPoint X="200" Y="200"/>
        </StylusPointsCollection>
    </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>

代码应为:

var stroke = (Stroke) ((ObjectDataProvider)XamlReader.Parse(xamlStr)).Data;

HTH.

这篇关于将XamlReader用于没有默认构造函数的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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