Share via


Trigger 類別

定義

代表屬性條件以及符合條件時所執行動作的類別。

public ref class Trigger sealed : Microsoft::Maui::Controls::TriggerBase, Microsoft::Maui::Controls::Xaml::IValueProvider
[Microsoft.Maui.Controls.ContentProperty("Setters")]
[Microsoft.Maui.Controls.Xaml.AcceptEmptyServiceProvider]
public sealed class Trigger : Microsoft.Maui.Controls.TriggerBase, Microsoft.Maui.Controls.Xaml.IValueProvider
[<Microsoft.Maui.Controls.ContentProperty("Setters")>]
[<Microsoft.Maui.Controls.Xaml.AcceptEmptyServiceProvider>]
type Trigger = class
    inherit TriggerBase
    interface IValueProvider
Public NotInheritable Class Trigger
Inherits TriggerBase
Implements IValueProvider
繼承
屬性
實作

備註

類別 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 比較,來判斷何時叫用 setter。

Setters

取得 Setter 物件的清單,當 Property 命名的屬性變成等於 Value 時,便會套用。

TargetType

可附加這個 TriggerBase 物件的目標物件類型。

(繼承來源 TriggerBase)
Value

取得或設定屬性的值,由 Property 屬性命名,此值會導致套用 setter。

方法

ApplyBindings()

將所有目前的系結套用至 BindingContext

(繼承來源 BindableObject)
ClearValue(BindableProperty)

清除先前為可系結屬性設定的任何值。

(繼承來源 BindableObject)
ClearValue(BindablePropertyKey)

清除先前針對可系結屬性設定的任何值,其索引鍵所識別。

(繼承來源 BindableObject)
CoerceValue(BindableProperty)

強制指定可系結屬性的值。 這是藉由叫 BindableProperty.CoerceValueDelegate 用指定的可系結屬性來完成。

(繼承來源 BindableObject)
CoerceValue(BindablePropertyKey)

強制指定可系結屬性的值。 這是藉由叫 BindableProperty.CoerceValueDelegate 用指定的可系結屬性來完成。

(繼承來源 BindableObject)
GetValue(BindableProperty)

傳回包含在指定可系結屬性中的值。

(繼承來源 BindableObject)
IsSet(BindableProperty)

判斷可系結屬性是否存在且已設定值。

(繼承來源 BindableObject)
OnBindingContextChanged()

引發 BindingContextChanged 事件。

(繼承來源 BindableObject)
OnPropertyChanged(String)

引發 PropertyChanged 事件。

(繼承來源 BindableObject)
OnPropertyChanging(String)

引發 PropertyChanging 事件。

(繼承來源 BindableObject)
RemoveBinding(BindableProperty)

從可系結的屬性中移除先前設定的系結。

(繼承來源 BindableObject)
SetBinding(BindableProperty, BindingBase)

將系結指派給可系結的屬性。

(繼承來源 BindableObject)
SetValue(BindableProperty, Object)

設定指定之可系結屬性的值。

(繼承來源 BindableObject)
SetValue(BindablePropertyKey, Object)

設定指定之可系結屬性的值。

(繼承來源 BindableObject)
SetValueCore(BindableProperty, Object, SetValueFlags)

供 Microsoft.Maui.Controls 平台內部使用。

(繼承來源 BindableObject)
UnapplyBindings()

從目前內容中移除所有目前的系結。

(繼承來源 BindableObject)

事件

BindingContextChanged

發生於 BindingContext 屬性的值變更時。

(繼承來源 BindableObject)
PropertyChanged

當屬性值變更時發生。

(繼承來源 BindableObject)
PropertyChanging

當屬性值變更中時發生。

(繼承來源 BindableObject)

明確介面實作

IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

代表屬性條件以及符合條件時所執行動作的類別。

(繼承來源 BindableObject)
IValueProvider.ProvideValue(IServiceProvider)

代表屬性條件以及符合條件時所執行動作的類別。

擴充方法

GetPropertyIfSet<T>(BindableObject, BindableProperty, T)

代表屬性條件以及符合條件時所執行動作的類別。

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

代表屬性條件以及符合條件時所執行動作的類別。

SetAppThemeColor(BindableObject, BindableProperty, Color, Color)

代表屬性條件以及符合條件時所執行動作的類別。

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

建立並將繫結套用至屬性。

適用於

另請參閱