Is it possible to trigger an event using xml code

Dimitris Christoforidis 301 Reputation points
2021-12-23T10:29:36.257+00:00

Is it possible to trigger an event using xml code? Example when button is clicked to focus an Entry?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-12-26T01:09:21.833+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I wonder if it is possible to achieve this only using xml code.

    No, it's available. Because it lacks related events or properties of the Button and Entry.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-12-24T01:51:09.807+00:00

    To achieve tis function, we need to create a custom control. For example, add a bool property to the cusotm Button and Entry. Then change the appearance of the two views based on the property changes using Triggers.

    Here is the sample code, you could refer to it.

    public class CustomButton : Button
    {
        public bool IsClicked
        {
            set => SetValue(IsClickedProperty, value);
            get => (bool)GetValue(IsClickedProperty);
        }
        public static readonly BindableProperty IsClickedProperty = BindableProperty.Create(nameof(IsClicked), typeof(bool), typeof(CustomButton), null);
    
        public CustomButton()
        {
            this.Clicked += CustomButton_Clicked;
        }
    
        private void CustomButton_Clicked(object sender, EventArgs e)
        {
            IsClicked = true;
        }
    }
    
    public class CustomEntry : Entry
    {
        public bool _IsFocused //because Entry already has IsFocused property, but the property only provides the get method
        {
            set => SetValue(_IsFocusedProperty, value);
            get => (bool)GetValue(_IsFocusedProperty);
        }
        public static readonly BindableProperty _IsFocusedProperty = BindableProperty.Create(nameof(_IsFocused), typeof(bool), typeof(CustomEntry), propertyChanged: ValueChanged);
    
        public CustomEntry()
        {
            this.Unfocused += CustomEntry_Unfocused;
        }
    
        private void CustomEntry_Unfocused(object sender, FocusEventArgs e)
        {
            _IsFocused = false;
        }
    
        private static void ValueChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var customEntry = bindable as CustomEntry;
            if ((bool)newValue)
            {
                customEntry.Focus();
            }
        }
    }
    

    page.xaml

    <local:CustomButton x:Name="customButton">
        <local:CustomButton.Triggers>
            <DataTrigger TargetType="local:CustomButton" Binding="{Binding Source={x:Reference customEntry},Path=_IsFocused}" Value="False">
                <Setter Property="IsClicked" Value="False"/>
            </DataTrigger>
        </local:CustomButton.Triggers>
    </local:CustomButton>
    <local:CustomEntry x:Name="customEntry">
        <local:CustomEntry.Triggers>
            <DataTrigger TargetType="local:CustomEntry" Binding="{Binding Source={x:Reference customButton},Path=IsClicked}" Value="True">
                <Setter Property="_IsFocused" Value="True"/>
            </DataTrigger>
        </local:CustomEntry.Triggers>
    </local:CustomEntry>
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.