Share via


Trigger 类

定义

表示属性条件和在满足条件时所执行的操作的类。

[Xamarin.Forms.ContentProperty("Setters")]
[Xamarin.Forms.Xaml.AcceptEmptyServiceProvider]
public sealed class Trigger : Xamarin.Forms.TriggerBase, Xamarin.Forms.Xaml.IValueProvider
type Trigger = class
    inherit TriggerBase
    interface IValueProvider
继承
System.Object
Trigger
属性
实现

注解

Trigger 适用于检查已添加该属性的控件上任何属性的值。 也就是说,其默认绑定上下文是已将其添加到的控件。 若要绑定父级以外的控件,开发人员应改用 DataTrigger 类。

下面的 XML 示例提示用户键入机密,即“文本颜色为红色”。 当用户键入机密后,“输入”文本将颜色更改为红色。 如果用户随后将文本编辑为与机密不同,则代码将转回默认颜色。 此示例演示如何使用触发器绑定到封闭控件。


<?xml version="1.0" encoding="utf-8"?>
<ContentPage    xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:TriggerDemo"
                x:Class="TriggerDemo.TriggerDemoPage">
<StackLayout VerticalOptions="Center">
<Label Text="Tell me the secret"
               VerticalOptions="Center"
               HorizontalOptions="Center" />
<Entry Placeholder="Type the secret here."
               VerticalOptions="Center"
               HorizontalOptions="Center">
<Entry.Triggers>
<Trigger TargetType="Entry"
                    Property="Text"
                    Value="The text color is red">
<Setter Property="TextColor" Value="Red" />
</Trigger>
</Entry.Triggers>
</Entry>
</StackLayout>
</ContentPage>

以下示例演示开发人员如何使用触发器响应事件,并使用类更新控件属性 TriggerAction<T> 的值。 该示例提示用户回答有关文本颜色的问题,然后在用户键入“文本颜色为红色”时调用 ColorTriggerAction 以将文本变为红色。 开发人员应注意,虽然此示例不会在用户继续编辑字符串时将文本更改回默认颜色,但开发人员可以另外实现并指定退出操作以获取该结果。

在 命名空间中 TriggerDemo 定义了下面的 ColorTriggerAction 类:

public class ColorTriggerAction : TriggerAction<Entry>
{
    protected override void Invoke(Entry sender)
    {
        sender.TextColor = Color.Red;
    }
}

开发人员可以使用下面的 XAML 创建响应机密字符串的页面,方法是将文本区域的颜色 Entry 更改为红色。

<?xml version="1.0" encoding="utf-8"?>
<ContentPage    xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:TriggerDemo"
                x:Class="TriggerDemo.TriggerDemoPage">
<StackLayout VerticalOptions="Center">
<Label Text="What color is &quot;The text&quot;?"
               VerticalOptions="Center"
               HorizontalOptions="Center" />
<Entry Placeholder="Type answer here."
               VerticalOptions="Center"
               HorizontalOptions="Center"
               BackgroundColor="White">
<Entry.Triggers>
<Trigger TargetType="Entry"
                         Property="Text"
                         Value="The text is red" >
<Trigger.EnterActions>
<local:ColorTriggerAction />
</Trigger.EnterActions>
</Trigger>
</Entry.Triggers>
</Entry>
</StackLayout>
</ContentPage>

构造函数

Trigger(Type)

初始化一个新 Trigger 实例。

属性

BindingContext

获取或设置对象,该对象包含将被属于此 BindableObject 的绑定属性设定为目标的属性。

(继承自 BindableObject)
Dispatcher

表示属性条件和在满足条件时所执行的操作的类。

(继承自 BindableObject)
EnterActions

获取将在满足触发条件时调用的 TriggerAction 对象的列表。 忽略 EventTrigger 类。

(继承自 TriggerBase)
ExitActions

获取在不再满足触发条件时调用的 TriggerAction 对象的列表。 忽略 EventTrigger 类。

(继承自 TriggerBase)
IsSealed

获取一个值,该值指示触发器是否密封。

(继承自 TriggerBase)
Property

获取或设置其值将与 Value 进行比较以确定何时调用资源库的属性。

Setters

获取当 Property 命名的属性等于 Value 时将应用的 Setter 对象列表。

TargetType

TriggerBase 对象可附加到的对象类型。

(继承自 TriggerBase)
Value

获取或设置由 Property 属性命名的属性的值,它将导致资源库被应用。

方法

ApplyBindings()

将绑定应用到 BindingContext

(继承自 BindableObject)
ClearValue(BindableProperty)

清除由 SetValueproperty 设置的任何值。

(继承自 BindableObject)
ClearValue(BindablePropertyKey)

清除由 SetValuepropertyKey 标识的属性设置的任何值。

(继承自 BindableObject)
CoerceValue(BindableProperty)

表示属性条件和在满足条件时所执行的操作的类。

(继承自 BindableObject)
CoerceValue(BindablePropertyKey)

表示属性条件和在满足条件时所执行的操作的类。

(继承自 BindableObject)
GetValue(BindableProperty)

返回 BindableProperty 中包含的值。

(继承自 BindableObject)
GetValues(BindableProperty, BindableProperty)
已过时.

供 Xamarin.Forms 平台内部使用。

(继承自 BindableObject)
GetValues(BindableProperty, BindableProperty, BindableProperty)
已过时.

供 Xamarin.Forms 平台内部使用。

(继承自 BindableObject)
IsSet(BindableProperty)

如果目标属性存在并且已设置,则返回 true

(继承自 BindableObject)
OnBindingContextChanged()

替代此方法以在 BindingContext 更改时执行操作。

(继承自 BindableObject)
OnPropertyChanged(String)

从子类调用此方法以通知属性发生更改。

(继承自 BindableObject)
OnPropertyChanging(String)

从子类调用此方法以通知属性将要发生更改。

(继承自 BindableObject)
RemoveBinding(BindableProperty)

删除先前设置的绑定。

(继承自 BindableObject)
SetBinding(BindableProperty, BindingBase)

向属性分配绑定。

(继承自 BindableObject)
SetValue(BindableProperty, Object)

设置指定属性的值。

(继承自 BindableObject)
SetValue(BindablePropertyKey, Object)

设置 propertyKey 的值。

(继承自 BindableObject)
SetValueCore(BindableProperty, Object, SetValueFlags)

供 Xamarin.Forms 平台内部使用。

(继承自 BindableObject)
UnapplyBindings()

不应用所有以前设置的绑定。

(继承自 BindableObject)

事件

BindingContextChanged

只要 BindingContext 属性更改就会引发。

(继承自 BindableObject)
PropertyChanged

在属性已更改时引发。

(继承自 BindableObject)
PropertyChanging

在属性将要更改时引发。

(继承自 BindableObject)

显式接口实现

IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

供 Xamarin.Forms 平台内部使用。

(继承自 BindableObject)
IValueProvider.ProvideValue(IServiceProvider)

供 Xamarin.Forms 平台内部使用。

扩展方法

GetPropertyIfSet<T>(BindableObject, BindableProperty, T)

表示属性条件和在满足条件时所执行的操作的类。

SetAppThemeColor(BindableObject, BindableProperty, Color, Color)

表示属性条件和在满足条件时所执行的操作的类。

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

创建绑定并将其应用到属性。

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)
已过时.

通过表达式创建并应用绑定。

SetOnAppTheme<T>(BindableObject, BindableProperty, T, T)

表示属性条件和在满足条件时所执行的操作的类。

适用于

另请参阅