Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using ReXamForms.util;
- using Xamarin.Forms;
- namespace ReXamForms.ui
- {
- public class TriggersPage : ContentPage
- {
- public TriggersPage()
- {
- initUi();
- }
- private void initUi()
- {
- Title = "Triggers";
- BackgroundColor = Color.FromHex(AppConstant.COLOR_PAGE_BACKGROUND);
- Label propertyLabel = new Label
- {
- HorizontalOptions = LayoutOptions.Start,
- Text = "Property Trigger - Background changes to DimGray on Focus.",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
- FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
- };
- Entry myEntry1 = new Entry
- {
- Placeholder = "Tap to focus...",
- PlaceholderColor = Color.White,
- BackgroundColor = Color.FromHex("#484848"),
- TextColor = Color.White,
- Margin = new Thickness(0, 0, 0, 20)
- };
- //PROPERTY TRIGGER
- Trigger propertyTrigger = new Trigger(typeof(Entry));
- propertyTrigger.Property = IsFocusedProperty;
- propertyTrigger.Value = true;
- Setter setter = new Setter();
- setter.Property = BackgroundColorProperty;
- setter.Value = Color.DimGray;
- propertyTrigger.Setters.Add(setter);
- myEntry1.Triggers.Add(propertyTrigger);//entry focus .. changes entry background color.
- //END PROPERTY TRIGGER
- Label dataLabel = new Label
- {
- HorizontalOptions = LayoutOptions.Start,
- Text = "Data Trigger - Button is disabled if entry is empty.",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
- FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
- };
- Entry myEntry2 = new Entry
- {
- Placeholder = "Enter something and delete...",
- PlaceholderColor = Color.White,
- BackgroundColor = Color.FromHex("#484848"),
- TextColor = Color.White,
- Margin = new Thickness(0, 0, 0, 10)
- };
- Button triggerButton = new Button
- {
- Text = "Data Trigger",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold,
- FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button)),
- BackgroundColor = Color.FromHex("#484848"),
- Margin = new Thickness(0, 0, 0, 20)
- };
- triggerButton.Clicked += (sender, e) => { Console.WriteLine("Data Trigger button clicked."); };
- //DATA TRIGGER
- DataTrigger dataTrigger = new DataTrigger(typeof(Button));
- Binding dataBinding = new Binding();
- dataBinding.Source = myEntry2;
- dataBinding.Path = "Text.Length";
- dataTrigger.Binding = dataBinding;
- dataTrigger.Value = 0;
- Setter dataSetter1 = new Setter();
- dataSetter1.Property = IsEnabledProperty;
- dataSetter1.Value = false;
- Setter dataSetter2 = new Setter();
- dataSetter2.Property = Button.TextProperty;
- dataSetter2.Value = "Data Trigger Enforced.";
- dataTrigger.Setters.Add(dataSetter1);
- dataTrigger.Setters.Add(dataSetter2);
- triggerButton.Triggers.Add(dataTrigger);//if entry text > 0 enabled else disabled.
- //END DATA TRIGGER
- Label eventLabel = new Label
- {
- HorizontalOptions = LayoutOptions.Start,
- Text = "Event Trigger - Text color changes on validation.",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
- FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
- };
- Entry myEntry3 = new Entry
- {
- Placeholder = "Green = OK, Red = Error.",
- PlaceholderColor = Color.White,
- BackgroundColor = Color.FromHex("#484848"),
- TextColor = Color.White,
- Margin = new Thickness(0, 0, 0, 20)
- };
- //EVENT TRIGGER
- EventTrigger entryEventTrigger = new EventTrigger();
- entryEventTrigger.Event = "TextChanged";
- entryEventTrigger.Actions.Add(new NumericValidationTriggerAction());
- myEntry3.Triggers.Add(entryEventTrigger);//entry TextChanged event .. checks entered valid double and changes entry color.
- //END EVENT TRIGGER
- Label multiLabel = new Label
- {
- HorizontalOptions = LayoutOptions.Start,
- Text = "Multi Trigger - Button enabled on validation.",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold | FontAttributes.Italic,
- FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
- };
- Entry myEntry4 = new Entry
- {
- Placeholder = "Enter 2 characters...",
- PlaceholderColor = Color.White,
- BackgroundColor = Color.FromHex("#484848"),
- TextColor = Color.White,
- Margin = new Thickness(0, 0, 0, 10)
- };
- Entry myEntry5 = new Entry
- {
- Placeholder = "Enter 4 characters...",
- PlaceholderColor = Color.White,
- BackgroundColor = Color.FromHex("#484848"),
- TextColor = Color.White,
- Margin = new Thickness(0, 0, 0, 10)
- };
- Button multiTriggerButton = new Button
- {
- Text = "Multi Trigger",
- TextColor = Color.White,
- FontAttributes = FontAttributes.Bold,
- FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button)),
- BackgroundColor = Color.FromHex("#484848"),
- Margin = new Thickness(0, 0, 0, 10),
- IsEnabled = false
- };
- multiTriggerButton.Clicked += (sender, e) => { Console.WriteLine("Multi Trigger button clicked."); };
- //MULTI TRIGGER
- MultiTrigger multiTrigger = new MultiTrigger(typeof(Button));
- BindingCondition bindingCondition1 = new BindingCondition()
- {
- Binding = new Binding("Text") { Path = "Text.Length", Source = myEntry4 },
- Value = "2"
- };
- BindingCondition bindingCondition2 = new BindingCondition()
- {
- Binding = new Binding("Text") { Path = "Text.Length", Source = myEntry5 },
- Value = "4"
- };
- multiTrigger.Conditions.Add(bindingCondition1);
- multiTrigger.Conditions.Add(bindingCondition2);
- Setter multiSetter1 = new Setter();
- multiSetter1.Property = IsEnabledProperty;
- multiSetter1.Value = true;
- Setter multiSetter2 = new Setter();
- multiSetter2.Property = BackgroundColorProperty;
- multiSetter2.Value = Color.Purple;
- multiTrigger.Setters.Add(multiSetter1);
- multiTrigger.Setters.Add(multiSetter2);
- multiTriggerButton.Triggers.Add(multiTrigger);//button is enabled if both entry are of length =2 and =4 respectively.
- //END MULTI TRIGGER
- FlexLayout mainContainerFlexLayout = new FlexLayout
- {
- Direction = FlexDirection.Column,
- Margin = new Thickness(25, 10, 25, 0),
- Children = { propertyLabel, myEntry1, dataLabel, myEntry2, triggerButton, eventLabel, myEntry3, multiLabel, myEntry4, myEntry5, multiTriggerButton }
- };
- Content = mainContainerFlexLayout;
- }
- }
- //Class for Event Triggers.
- public class NumericValidationTriggerAction : TriggerAction<Entry>
- {
- protected override void Invoke(Entry entry)
- {
- double result;
- bool isValid = Double.TryParse(entry.Text, out result);
- entry.TextColor = isValid ? Color.GreenYellow : Color.Red;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement