MarkupExtension 声明中的默认构造函数参数 [英] Default Constructor Parameter in MarkupExtension declaration

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

问题描述

将这个问题减少到最低限度,考虑这个 MarkupExtension 类...

Reducing this question to the bare minimum, consider this MarkupExtension class...

public class ProblemStatement : MarkupExtension
{
    private readonly string _first;
    private readonly string _second;
    public ProblemStatement(string first, string second)
    {
        _first = first;
        _second = second;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
    public override string ToString()
    {
        return _first + _second;
    }
}

当这个 Xaml 被声明时...

When this Xaml is declared...

<Grid>
    <TextBlock Name="TextBlock1" Tag="{so:ProblemStatement 'hello', 'world'}"/>
    <TextBlock Text="{Binding ElementName=TextBlock1, Path=Tag}"/>
</Grid>

...您会按预期在 TextBlock 中看到helloworld".到目前为止一切顺利.

...you see 'helloworld' in the TextBlock as expected. All's well to this point.

但是将构造函数参数更改为此...

But changing the constructor parameter to this...

public ProblemStatement(string first, string second = "nothing")

...以及与此相关的 Xaml...

...and the relevant Xaml to this...

   <Grid>
        <TextBlock Name="TextBlock1" Tag="{so:ProblemStatement 'hello'}"/>
        <TextBlock Text="{Binding ElementName=TextBlock1, Path=Tag}"/>
    </Grid>

产生的错误信息是...

the resulting error message is...

No constructor for type 'ProblemStatement' has 1 parameters.

有一个变通方法,就是通过将这个语句添加到类中来链接构造函数...

There is a work-around, which is to chain the constructor by adding this statement to the class...

public ProblemStatement(string first) : this(first, "not provided") { }

这将在 TextBlock 中显示hellonot provided".然而,这也改变了 MarkupExtension 的语义,在更大的真实世界"情况下是不可取的.当使用更复杂的类型或构造函数参数是动态"类型时,重载的复杂性也会显着增加.此外,例如,完全禁止使用新的来电者信息"属性.

and this will show 'hellonot provided' in the TextBlock. However, this also changes the semantics of the MarkupExtension and is not desirable in the larger, 'real-world' case. Also the complexity of overloading increases dramatically when more complex types are used or the constructor arguments are of type 'dynamic'. Also, for example, use of the new 'Caller Information' attributes is blocked altogether.

所以问题是:如何声明 Xaml 以便 Xaml 解析器接受默认构造函数参数?

So the question is: how to declare the Xaml so that the Xaml parser will honour a default constructor argument?

推荐答案

试试这个:

    public string Optional{ get; set; } = "DefaultValue";

    private readonly string _mandatory;

    public ProblemStatement(string mandatory)
    {
        _mandatory = mandatory;
    }

用法:

<TextBlock Name="TextBlock1" Tag="{local:ProblemStatement 'hello', Optional=NotDefault}"/>

替代方案:

<TextBlock Name="TextBlock1" Tag="{local:ProblemStatement 'hello'}"/>

结果:

  • 没有 XAML 解析错误
  • 无需为可选参数重载构造函数
  • 强制参数是构造函数参数.
  • 可选参数是属性.

这篇关于MarkupExtension 声明中的默认构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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