Xamarin.Forms MarkupExtension用于绑定 [英] Xamarin.Forms MarkupExtension for binding

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

问题描述

我想扩展标记以简化出价. 我有字典,并将该属性绑定到视图中的Label. 我有ValueConverter接受此指令,然后传递ConverterParameter(它是一个字符串,它会找到

I want to make markup extension to simplify bidning. I have dictionary and I bind that property to a Label in the view. I have ValueConverter that takes this dictinary and I pass ConverterParameter which is a string and it finds

<Label Text="{Binding Tanslations,Converter={StaticResource TranslationWithKeyConverter}, ConverterParameter='Test'}"/>

但是我必须对不同的标签执行相同的操作,但是键(ConverterParameter)将有所不同,其余的将保持不变

but I have to do same thing for different labels but the key (ConverterParameter) will be different, the rest will remain same

我想要一个允许我写这个的markupextension:

I want a markupextension that will allow me to write this:

<Label Text="{local:MyMarkup Key=Test}"/>

此标记应使用TranslationWithKeyConverter的valueconverter和名为Key的ConverterParameter生成对名为"Tanslations"的属性的绑定.

this markup should generate binding to the property named "Tanslations" with valueconverter of TranslationWithKeyConverter and ConverterParameter with value of Key.

我尝试这样做,但是不起作用:

I tried to do it but it does not work:

public class WordByKey : IMarkupExtension
{
    public string Key { get; set; }
    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return new Binding("Tanslations", BindingMode.OneWay, converter: new TranslationWithKeyConverter(), converterParameter: Key);
    }
}

标签上什么都没有显示.

nothing is displayed on the label.

推荐答案

让我们从一个明显的警告开始:您不应仅仅因为它简化了语法就编写了自己的MarkupExtensions. XF Xaml解析器和XamlC编译器可以对已知的MarkupExtensions做一些优化技巧,但对您却不起作用.

Let's start with the obvious warning: you shouldn't write your own MarkupExtensions only because it simplifies the syntax. The XF Xaml parser, and the XamlC Compiler can do some optimization tricks on known MarkupExtensions, but can't on yours.

现在我们已经警告您,我们可以继续前进.

Now that you're warned, we can move on.

如果您使用正确的名称(与您粘贴的名称不同),则您所做的工作可能适用于普通Xaml解析器),但肯定不会在XamlC处于打开状态时使用.代替实现IMarkupExtension,您应该像这样实现IMarkupExtension<BindingBase>:

What you do probably works for the normal Xaml parser provided you use the correct names, unlike what you pasted) but certainly does not with XamlC turned on. Instead of implementing IMarkupExtension, you should implement IMarkupExtension<BindingBase> like this:

[ContentProperty("Key")]
public sealed class WordByKeyExtension : IMarkupExtension<BindingBase>
{
    public string Key { get; set; }
    static IValueConverter converter = new TranslationWithKeyConverter();

    BindingBase IMarkupExtension<BindingBase>.ProvideValue(IServiceProvider serviceProvider)
    {
        return new Binding("Tanslations", BindingMode.OneWay, converter: converter, converterParameter: Key);
    }

    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
    {
        return (this as IMarkupExtension<BindingBase>).ProvideValue(serviceProvider);
    }
}

然后您可以在其中使用它:

and then you can use it like in:

<Label Text="{local:WordByKey Key=Test}"/>

<Label Text="{local:WordByKey Test}"/>

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

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