首页 > 解决方案 > 自定义视图上的多重触发器

问题描述

我创建了一个名为“InfoButton”的自定义视图:

public class InfoButton : ImageButton
{
    public string Glyph { get; set; } = "\U000F02FD";
    public string Title { get; set; }
    public string Text { get; set; }
    public InfoButton()
    {
        Source = new FontImageSource() { Glyph = Glyph, FontFamily = "materialdesign.ttf#materialdesign", Size= (double)new FontSizeConverter().ConvertFromInvariantString("Title"), Color = Color.Black };
        BackgroundColor = Color.Transparent;
        Clicked += InfoButton_Clicked;
    }

    private void InfoButton_Clicked(object sender, EventArgs e)
    {
        AlertPopup.DisplayAlertPopup(Title, Text, 0, "Close");
    }
}

现在我为它创建了一个触发器:

<models:InfoButton Glyph="&#xF059F;" Title="some title" Text="some text" IsVisible="false" HorizontalOptions="EndAndExpand">
    <models:InfoButton.Triggers>
        <MultiTrigger TargetType="{x:Type models:InfoButton}">
            <MultiTrigger.Conditions>
                <BindingCondition Binding="{Binding isPublic}" Value="true"/>
                <BindingCondition Binding="{Binding isReadonly}" Value="false"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="IsVisible" Value="true"/>
                <Setter Property="Glyph" Value="&#xF059F;"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </models:InfoButton.Triggers>
</models:InfoButton>

但是我在编译时遇到了这个错误:

XFC0001 无法解析类型“InfoButton(属性缺失或缺失访问器)”上的属性“Glyph”。

可能是什么问题呢?

谢谢你的帮助。

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


要简单地解决您的问题,请转到解决方案部分。要了解您的代码失败的原因,请参阅说明部分。

解决方案

您必须将您的Glyph属性转换为BindableProperty,如下所示

public static readonly BindableProperty GlyphProperty = BindableProperty.Create(nameof(Glyph), typeof(string), typeof(InfoButton), "\U000F02FD");

public string Glyph
{
    get { return (string)GetValue(GlyphProperty); }
    set { SetValue(GlyphProperty, value); }
}

解释

Glyph可以在 Setter.Property 的文档中找到为什么您的代码失败并定义为简单属性解释

只能使用 Setter 设置可绑定的属性。


推荐阅读