具有MarkupExtension的IValueConverter [英] IValueConverter with MarkupExtension

查看:97
本文介绍了具有MarkupExtension的IValueConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我读到一个 IValueConverter ,它也继承自 MarkupExtension 。就像这样:

Recently I read about an IValueConverter which also inherits from MarkupExtension. It was something like:

internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    private static BoolToVisibilityConverter converter;
    public BoolToVisibilityConverter()
    {
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
            {
                return Visibility.Visible;
            }
        }
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility)
        {
            Visibility visibility = (Visibility)value;
            if (visibility == Visibility.Collapsed)
            {
                return false;
            }
        }
        return true;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return converter ?? (converter = new BoolToVisibilityConverter());
    }
}

用法如下:

<Button Content="Delete" Visibility="{Binding CanDelete, UpdateSourceTrigger=PropertyChanged, Converter={local:BoolToVisibilityConverter}"/>

我曾经使用资源中的转换器,例如:

I was used to use converters from a Resource like:

<loc:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
...
<Button Content="Delete" Visibility="{Binding CanDelete, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BoolToVisibilityConverter}"/>

我的第一个问题是:什么是更好的方法?如果我使用 MarkupExtension-Version (除了用法更容易键入),它有什么优势?

My first question now is: What is the better way? What advantages does it have if I'm using the MarkupExtension-Version (Beside the usage is easier to type)?

我也看到了一个非常类似的实现,它看起来像:

I also saw a very similar implementation which looks like:

internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    public BoolToVisibilityConverter()
    {
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
            {
                return Visibility.Visible;
            }
        }
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility)
        {
            Visibility visibility = (Visibility)value;
            if (visibility == Visibility.Collapsed)
            {
                return false;
            }
        }
        return true;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;        
    }
}

如果我理解正确,第一个解决方案只会创建此转换器的一个实例。第二个为每个XAML创建此转换器的新实例,对吧?

If I understand it right, the first solution only creates one instance of this converter. The second one creates for every XAML a new instance of this converter, right?

推荐答案

唯一的优点是标记在这种情况下,扩展名提供的是更简洁的XAML语法。

The only (slight) advantage that the markup extension is providing in this case is more concise XAML syntax.

代替此:

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
...
{Binding SomeBooleanProperty, Converter={StaticResource BooleanToVisibilityConverter}}

您可以使用:

{Binding SomeBooleanProperty, Converter={my:BoolToVisibilityConverter}}

在我看来,这确实不值得。如果您担心保存击键,则可以缩短用于引用转换器的键:

In my opinion it's not really worth it. If you were that bothered about saving keystrokes you could just shorten the key used to reference the converter:

<BooleanToVisibilityConverter x:Key="btvc" />
...
{Binding SomeBooleanProperty, Converter={StaticResource my:btvc}}






由于标记扩展的 ProvideValue 方法是 instance 方法,因此只能一旦创建了该类的实例,该函数将被调用。由于该类既是标记扩展又是转换器,因此代码的两个变体每次都会创建一个转换器。唯一的不同是,第一个变体将始终返回相同的转换器:但是,不会阻止创建另一个转换器。


As the ProvideValue method of the markup extension is an instance method, it can only be called once an instance of the class has been created. As the class is both a markup extension and a converter, both variants of the code will create a converter each time. The only difference is that the first variant will always return the same converter: it won't however, stop another converter from being created.

这篇关于具有MarkupExtension的IValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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