Xamarin Forms Event Handler not firing

Gordon S 501 Reputation points

I have a Base ViewModel with a "SetProperty" function which calls an "OnPropertyChanged" function which invokes a PropertyChangedEventHandler event.

            protected bool SetProperty<T>(ref T backingStore, T value,
                [CallerMemberName] string propertyName = "",
                Action onChanged = null)
                if (EqualityComparer<T>.Default.Equals(backingStore, value))
                    return false;

                backingStore = value;
                return true;

            #region INotifyPropertyChanged

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
                var changed = PropertyChanged;
                if (changed == null)

                changed.Invoke(this, new PropertyChangedEventArgs(propertyName));

I have a "partial" view which is used to show a message and its visibility is controlled by IsVisible (code trimmed for simplicity).
*Note Style is misspelt because spelling it right was stopping me posting this question!?

<StackLayout IsVisible="{Binding ActionMessageVisible}">
    <yummy:PancakeView Stylee = "{StaticResource PancakeNotificationTask}">
                    <Span Text="{Binding ActionMessageText}" />

In the code-behind I have code to attach to, and deal with the PropertyChanged event.

        public partial class ActionMessageView : StackLayout
            public ActionMessageView()

                var vm = new BaseViewModel();
                vm.PropertyChanged += VmOnPropertyChanged;

            private void VmOnPropertyChanged(object sender, PropertyChangedEventArgs e)
                if (e.PropertyName != "ActionMessageVisible") return;

                var viewModel = (BaseViewModel) sender;
                if (viewModel.ActionMessageVisible)
                    MessageLayout.FadeTo(1, 5000, Easing.SpringIn);

The problem is that the VmOnPropertyChanged function is never called. Anyone know why? Is what I'm trying to do actually possible? Is there a better way of doing it?

Basically, when the "IsVisible" property is set to True, I want the content of the StackLayout to fade in - or even, the entire StackLayout to fade in. I then want it to fade out again after "n" seconds, but that's another issue!

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points


    Welcome to our Microsoft Q&A platform!

    What control is 'MessageLayout'? According to your posted code, it seems that you want to execute an animation when a view become visible\invisible. To achieve this, try creating a custom control to add a custom property and specify a PropertyChanged event to detect the changes of the property.

    Check the code:

       public class CustomLayout : StackLayout  
           static CustomLayout layout;  
           public CustomLayout()  
               layout = this;  
           public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(CustomLayout), null, propertyChanged: OnEventNameChanged);  
           public bool IsSelected  
               set => SetValue(IsSelectedProperty, value);  
               get => (bool)GetValue(IsSelectedProperty);  
           private static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)  
               var value = (bool)newValue;  
               if (value)  
                   layout.FadeTo(1, 5000, Easing.SpringOut);  
                   layout.FadeTo(1, 5000, Easing.SpringIn);  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    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 additional answers

Sort by: Most helpful