自定义视图上的多重触发器
multitrigger on custom view
我创建了一个名为“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 Cannot resolve property "Glyph" on type "InfoButton (property missing or missing accessors)".
可能是什么问题?
感谢您的帮助。
要简单地解决您的问题,请转到 解决方案 部分。要了解您的代码失败的原因,请参阅 说明 部分。
解决方案
您必须将 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
定义为简单 属性 的代码失败的解释
Only bindable properties can be set with a Setter.
我创建了一个名为“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 Cannot resolve property "Glyph" on type "InfoButton (property missing or missing accessors)".
可能是什么问题?
感谢您的帮助。
要简单地解决您的问题,请转到 解决方案 部分。要了解您的代码失败的原因,请参阅 说明 部分。
解决方案
您必须将 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
定义为简单 属性 的代码失败的解释
Only bindable properties can be set with a Setter.