【问题标题】:multitrigger on custom view自定义视图上的多重触发器
【发布时间】:2021-11-14 06:46:55
【问题描述】:

我创建了一个名为“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# xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

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

    解决方案

    您必须将您的 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); }
    }
    

    说明

    可以在the docu for Setter.Property 中找到解释为什么您的代码因将Glyph 定义为简单属性而失败的原因

    只有可绑定的属性可以用 Setter 设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多