Hover Effect is not working properly on button in Xamarin UWP

XamarinBeginnerXamarinBeginner Member ✭✭
edited December 2018 in Xamarin.Forms

I tried to add a hover effect for a button.
Following code shows a custom class created in UWP

[assembly: ResolutionGroupName("MyCompany")]
[assembly: ExportEffect(typeof(FocusEffect), "FocusEffect")]
namespace EffectsSample.UWP
{
    class FocusEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            try
            {
                (Control as Windows.UI.Xaml.Controls.Control).PointerEntered += pointer_Entered;    
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Cannot set property on attached control. Error: ", ex.Message);
            }
        }
        private void pointer_Entered(object sender, RoutedEventArgs e)
        {           
            (this.Control as Windows.UI.Xaml.Controls.Control).Background = new SolidColorBrush(Colors.Red);   
        }         
        protected override void OnDetached()
        {
        }
       }
    }

Effect is added to button in xaml like below:

   <Button Text="Button">
        <Button.Effects>
            <local:FocusEffect />
        </Button.Effects>
    </Button>

The issue is that background is not changing on Pointer Enter Event. Instead it changes on Pointer Exit.
Any help will be appreciated!

Answers

Sign In or Register to comment.