Xamarin Trigger(二)Property Trigger & Event Trigger & Multi Trigger

  • 151
  • 0
  • 2017-02-01

    

Event Trigger: 當Control 的Event 發生時觸發Trigger

Property Trigger: 當Control 的Property 發生時觸發另一個屬性變動

Multi Trigger: 所有條件都成立則執行Action,例如帳號的欄位跟Password欄位 都有輸入則button 的IsEnabled打開

<?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:DifferenceUI"
             xmlns:vm="clr-namespace:DifferenceUI.ViewModel;assembly:DifferenceUI"
             x:Class="DifferenceUI.PropertyTriggerPage"
             x:Name="PropertyPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <vm:BooleanConverter x:Key="BooleanConverter"></vm:BooleanConverter>
            <!--Style 的繼承透過BaseOn-->
            <Style TargetType="View"  x:Key="CommonStyle">
                <Setter Property="VerticalOptions" Value="CenterAndExpand"></Setter>
                <Setter Property="HorizontalOptions" Value="FillAndExpand"></Setter>
            </Style>
            <Style TargetType="Button" x:Key="CommonButtonStyle" BasedOn="{StaticResource CommonStyle}">
            </Style>
            <Style TargetType="Entry" x:Key="CommonEntryStyle" BasedOn="{StaticResource CommonStyle}">
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
        <Label Style="CommonStyle"  Text="Please Login to Xamarin World" FontSize="Large" HorizontalTextAlignment="Center"></Label>
        <Entry Style="CommonEntryStyle" x:Name="name" Text="{Binding Name}" Placeholder="Login Name"></Entry>
        <Entry Style="CommonEntryStyle" x:Name="pass" Text="{Binding PassWord}" Placeholder="PassWord" IsPassword="True"></Entry>
        <Button Style="CommonButtonStyle" x:Name="btn" Text="Login" IsEnabled="False">
            <Button.Triggers>
                <MultiTrigger TargetType="Button">
                    <!---以下2個條件都成立Button 的IsEnabled=true才成立-->
                    <MultiTrigger.Conditions>
                        <!--Binding 叫name的Entry 物件的Text.Length屬性透過Converter 轉成True or False 決定Button的IsEnabled-->
                        <BindingCondition Binding="{Binding Source={x:Reference name},Path=Text.Length,Converter={StaticResource BooleanConverter}}"
                               Value="true" />
                        <BindingCondition Binding="{Binding Source={x:Reference pass},Path=Text.Length,Converter={StaticResource BooleanConverter}}"                              
                               Value="true" />
                    </MultiTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True" />
                </MultiTrigger>
                <EventTrigger Event="Clicked">
                    <vm:NumericValidationTriggerAction Page="{x:Reference PropertyPage}">
                    </vm:NumericValidationTriggerAction>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </StackLayout>
</ContentPage>

宣告一個類別繼承TriggerAction並且實做Invoke方法,傳入ContentPage為了取得ViewModel

(這裡不知道怎麼直接傳入ViewModel,如果知道的朋友請告知),

若密碼錯誤則提示視窗,正確則跳下一頁面


namespace DifferenceUI.ViewModel
{
    public class PropertyTriggerViewModel
    {
        private String _name, _password;
        public String Name { set; get; }
        public String PassWord { set; get; }

        public ContentPage MyPage { set; get; }

        public void CheckRule()
        {
            if (String.Equals(Name, "Xamarin") && String.Equals(PassWord, "1234"))
            {
                MyPage.Navigation.PushModalAsync(new DataTriggerPage());
            }
            else
            {
                MyPage.DisplayAlert("Information", "密碼錯誤", "Retry");
            }
        }
        public ICommand LoginCommand
        {
            get
            {

                Command command = new Command(() =>
                {

                    CheckRule();
                });
                return command;
            }
        }   
    }

    public class BooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            
            if (value.GetType() == typeof(Int32))
            {
                if ((int)value > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    
    public class NumericValidationTriggerAction : TriggerAction<Button>
    {
        public ContentPage Page { set; get; }

        protected override void Invoke(Button entry)
        {
            PropertyTriggerViewModel vm= Page.BindingContext as PropertyTriggerViewModel;
            vm.CheckRule();   
        }
    }
}