Share via

Xamarin Forms Event Handler not firing

Gordon S 501 Reputation points
2021-07-06T13:47:33.607+00:00

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;
                onChanged?.Invoke();
                OnPropertyChanged(propertyName);
                return true;
            }

            #region INotifyPropertyChanged

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

                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}">
            <Label.FormattedText>
                <FormattedString>
                    <Span Text="{Binding ActionMessageText}" />
                </FormattedString>
            </Label.FormattedText>
    </yummy:PancakeView>
</StackLayout>

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

        public partial class ActionMessageView : StackLayout
        {
            public ActionMessageView()
            {
                InitializeComponent();

                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!

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JarvanZhang 23,971 Reputation points
2021-07-07T09:18:58.3+00:00

Hello,​

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);  
           }  
           else  
           {  
               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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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