c# - 自定义视图上的多重触发器
问题描述
我创建了一个名为“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="󰖟" 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="󰖟"/>
</MultiTrigger.Setters>
</MultiTrigger>
</models:InfoButton.Triggers>
</models:InfoButton>
但是我在编译时遇到了这个错误:
XFC0001 无法解析类型“InfoButton(属性缺失或缺失访问器)”上的属性“Glyph”。
可能是什么问题呢?
谢谢你的帮助。
解决方案
要简单地解决您的问题,请转到解决方案部分。要了解您的代码失败的原因,请参阅说明部分。
解决方案
您必须将您的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 设置可绑定的属性。
推荐阅读
- docker - gitlab 管道:在 gitlab-ci.yml 中获取主机名
- javascript - Javascript 中的对象赋值
- python - How to design realtime deeplearnig application for robotics using python?
- javascript - c# webbrowser javascript 处理文件下载和保存文件没有对话框
- php - 如何使用变量中的序列为每条记录一一更新表php
- c++ - :-1: 错误: LNK1104: 无法打开文件'libboost_date_time-vc141-mt-x64-1_71.lib'
- java - 如何使用 swagger 为同一对象提供多个示例值
- angular - 类型“{}”上不存在属性“订阅”
- snowflake-cloud-data-platform - 雪花;SQL 编译错误:无效的对象类型:GET_DDL 上的“EXTERNAL_TABLE”(“DATABASE”,“MyDb”)
- java - 即使资源存在于可绘制文件夹中,资源未找到异常(无法找到资源 ID)