Tuesday, May 26, 2015

CallDataMethod trigger in Xamarin Forms

Here’s a very simple implementation of a CallDataMethod trigger in Xamarin Forms:
public class CallDataMethod: TriggerAction<VisualElement> { public string Method { get; set; } #region implemented abstract members of TriggerAction protected override void Invoke (VisualElement sender) { MethodInfo method = sender.GetType ().GetRuntimeMethod (Method, new Type[0]); if (method != null) { ParameterInfo[] parameters = method.GetParameters (); if (parameters.Length == 0) method.Invoke (sender, null); } } #endregion }

For a demo, let’s say we have two text-boxes (Entry control in Xamarin Forms) and you want to enable the second one when the first has ten characters:

<?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:controls="clr-namespace:Xamore.Controls;assembly=Xamore.Controls" x:Class="Xamore.Controls.TestApp.Views.CallDataMethodPage"> <StackLayout> <Entry x:Name="FirstEntry" WidthRequest="100" /> <Entry IsEnabled="false" WidthRequest="100"> <Entry.Triggers> <DataTrigger TargetType="Entry" Binding="{Binding Source={x:Reference FirstEntry}, Path=Text.Length}" Value="10"> <Setter Property="IsEnabled" Value="True" /> <Trigger.EnterActions> <controls:CallDataMethod Method="Focus" /> </Trigger.EnterActions> </DataTrigger> </Entry.Triggers> </Entry> </StackLayout> </ContentPage>

No comments:

Post a Comment