Advertisement
Guest User

Xamarin Forms : Triggers {Programmatically}

a guest
Dec 26th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.48 KB | None | 0 0
  1. using System;
  2. using ReXamForms.util;
  3. using Xamarin.Forms;
  4.  
  5. namespace ReXamForms.ui
  6. {
  7.     public class TriggersPage : ContentPage
  8.     {
  9.         public TriggersPage()
  10.         {
  11.             initUi();
  12.         }
  13.  
  14.         private void initUi()
  15.         {
  16.             Title = "Triggers";
  17.             BackgroundColor = Color.FromHex(AppConstant.COLOR_PAGE_BACKGROUND);
  18.  
  19.             Label propertyLabel = new Label
  20.             {
  21.                 HorizontalOptions = LayoutOptions.Start,
  22.                 Text = "Property Trigger - Background changes to DimGray on Focus.",
  23.                 TextColor = Color.White,
  24.                 FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
  25.                 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
  26.             };
  27.  
  28.             Entry myEntry1 = new Entry
  29.             {
  30.                 Placeholder = "Tap to focus...",
  31.                 PlaceholderColor = Color.White,
  32.                 BackgroundColor = Color.FromHex("#484848"),
  33.                 TextColor = Color.White,
  34.                 Margin = new Thickness(0, 0, 0, 20)
  35.             };
  36.             //PROPERTY TRIGGER
  37.             Trigger propertyTrigger = new Trigger(typeof(Entry));
  38.             propertyTrigger.Property = IsFocusedProperty;
  39.             propertyTrigger.Value = true;
  40.  
  41.             Setter setter = new Setter();
  42.             setter.Property = BackgroundColorProperty;
  43.             setter.Value = Color.DimGray;
  44.  
  45.             propertyTrigger.Setters.Add(setter);
  46.             myEntry1.Triggers.Add(propertyTrigger);//entry focus .. changes entry background color.
  47.             //END PROPERTY TRIGGER
  48.  
  49.             Label dataLabel = new Label
  50.             {
  51.                 HorizontalOptions = LayoutOptions.Start,
  52.                 Text = "Data Trigger - Button is disabled if entry is empty.",
  53.                 TextColor = Color.White,
  54.                 FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
  55.                 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
  56.             };
  57.  
  58.             Entry myEntry2 = new Entry
  59.             {
  60.                 Placeholder = "Enter something and delete...",
  61.                 PlaceholderColor = Color.White,
  62.                 BackgroundColor = Color.FromHex("#484848"),
  63.                 TextColor = Color.White,
  64.                 Margin = new Thickness(0, 0, 0, 10)
  65.             };
  66.  
  67.             Button triggerButton = new Button
  68.             {
  69.                 Text = "Data Trigger",
  70.                 TextColor = Color.White,
  71.                 FontAttributes = FontAttributes.Bold,
  72.                 FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button)),
  73.                 BackgroundColor = Color.FromHex("#484848"),
  74.                 Margin = new Thickness(0, 0, 0, 20)
  75.             };
  76.             triggerButton.Clicked += (sender, e) => { Console.WriteLine("Data Trigger button clicked."); };
  77.  
  78.             //DATA TRIGGER
  79.             DataTrigger dataTrigger = new DataTrigger(typeof(Button));
  80.             Binding dataBinding = new Binding();
  81.             dataBinding.Source = myEntry2;
  82.             dataBinding.Path = "Text.Length";
  83.             dataTrigger.Binding = dataBinding;
  84.             dataTrigger.Value = 0;
  85.  
  86.             Setter dataSetter1 = new Setter();
  87.             dataSetter1.Property = IsEnabledProperty;
  88.             dataSetter1.Value = false;
  89.  
  90.             Setter dataSetter2 = new Setter();
  91.             dataSetter2.Property = Button.TextProperty;
  92.             dataSetter2.Value = "Data Trigger Enforced.";
  93.  
  94.             dataTrigger.Setters.Add(dataSetter1);
  95.             dataTrigger.Setters.Add(dataSetter2);
  96.             triggerButton.Triggers.Add(dataTrigger);//if entry text > 0 enabled else disabled.
  97.             //END DATA TRIGGER
  98.  
  99.             Label eventLabel = new Label
  100.             {
  101.                 HorizontalOptions = LayoutOptions.Start,
  102.                 Text = "Event Trigger - Text color changes on validation.",
  103.                 TextColor = Color.White,
  104.                 FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
  105.                 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
  106.             };
  107.  
  108.             Entry myEntry3 = new Entry
  109.             {
  110.                 Placeholder = "Green = OK, Red = Error.",
  111.                 PlaceholderColor = Color.White,
  112.                 BackgroundColor = Color.FromHex("#484848"),
  113.                 TextColor = Color.White,
  114.                 Margin = new Thickness(0, 0, 0, 20)
  115.             };
  116.             //EVENT TRIGGER
  117.             EventTrigger entryEventTrigger = new EventTrigger();
  118.             entryEventTrigger.Event = "TextChanged";
  119.             entryEventTrigger.Actions.Add(new NumericValidationTriggerAction());
  120.             myEntry3.Triggers.Add(entryEventTrigger);//entry TextChanged event .. checks entered valid double and changes entry color.
  121.             //END EVENT TRIGGER
  122.  
  123.             Label multiLabel = new Label
  124.             {
  125.                 HorizontalOptions = LayoutOptions.Start,
  126.                 Text = "Multi Trigger - Button enabled on validation.",
  127.                 TextColor = Color.White,
  128.                 FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
  129.                 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
  130.             };
  131.  
  132.             Entry myEntry4 = new Entry
  133.             {
  134.                 Placeholder = "Enter 2 characters...",
  135.                 PlaceholderColor = Color.White,
  136.                 BackgroundColor = Color.FromHex("#484848"),
  137.                 TextColor = Color.White,
  138.                 Margin = new Thickness(0, 0, 0, 10)
  139.             };
  140.  
  141.             Entry myEntry5 = new Entry
  142.             {
  143.                 Placeholder = "Enter 4 characters...",
  144.                 PlaceholderColor = Color.White,
  145.                 BackgroundColor = Color.FromHex("#484848"),
  146.                 TextColor = Color.White,
  147.                 Margin = new Thickness(0, 0, 0, 10)
  148.             };
  149.  
  150.             Button multiTriggerButton = new Button
  151.             {
  152.                 Text = "Multi Trigger",
  153.                 TextColor = Color.White,
  154.                 FontAttributes = FontAttributes.Bold,
  155.                 FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button)),
  156.                 BackgroundColor = Color.FromHex("#484848"),
  157.                 Margin = new Thickness(0, 0, 0, 10),
  158.                 IsEnabled = false
  159.             };
  160.             multiTriggerButton.Clicked += (sender, e) => { Console.WriteLine("Multi Trigger button clicked."); };
  161.  
  162.             //MULTI TRIGGER
  163.             MultiTrigger multiTrigger = new MultiTrigger(typeof(Button));
  164.             BindingCondition bindingCondition1 = new BindingCondition()
  165.             {
  166.                 Binding = new Binding("Text") { Path = "Text.Length", Source = myEntry4 },
  167.                 Value = "2"
  168.             };
  169.  
  170.             BindingCondition bindingCondition2 = new BindingCondition()
  171.             {
  172.                 Binding = new Binding("Text") { Path = "Text.Length", Source = myEntry5 },
  173.                 Value = "4"
  174.             };
  175.  
  176.             multiTrigger.Conditions.Add(bindingCondition1);
  177.             multiTrigger.Conditions.Add(bindingCondition2);
  178.  
  179.             Setter multiSetter1 = new Setter();
  180.             multiSetter1.Property = IsEnabledProperty;
  181.             multiSetter1.Value = true;
  182.  
  183.             Setter multiSetter2 = new Setter();
  184.             multiSetter2.Property = BackgroundColorProperty;
  185.             multiSetter2.Value = Color.Purple;
  186.  
  187.             multiTrigger.Setters.Add(multiSetter1);
  188.             multiTrigger.Setters.Add(multiSetter2);
  189.             multiTriggerButton.Triggers.Add(multiTrigger);//button is enabled if both entry are of length =2 and =4 respectively.
  190.             //END MULTI TRIGGER
  191.  
  192.  
  193.  
  194.  
  195.             FlexLayout mainContainerFlexLayout = new FlexLayout
  196.             {
  197.                 Direction = FlexDirection.Column,
  198.                 Margin = new Thickness(25, 10, 25, 0),
  199.                 Children = { propertyLabel, myEntry1, dataLabel, myEntry2, triggerButton, eventLabel, myEntry3, multiLabel, myEntry4, myEntry5, multiTriggerButton }
  200.             };
  201.  
  202.             Content = mainContainerFlexLayout;
  203.         }
  204.     }
  205.  
  206.     //Class for Event Triggers.
  207.     public class NumericValidationTriggerAction : TriggerAction<Entry>
  208.     {
  209.         protected override void Invoke(Entry entry)
  210.         {
  211.             double result;
  212.             bool isValid = Double.TryParse(entry.Text, out result);
  213.             entry.TextColor = isValid ? Color.GreenYellow : Color.Red;
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement