xamarin/Xamarin.Forms

Different behaviour is reproduced when changing the Opacity for normal Button and Custom Button

Aperta

#5323 aperta il 20 feb 2019

 (1 commento) (0 reazioni) (0 assegnatari)C# (1926 fork)batch import
e/4 :clock4:help wantedinactivet/bug :bug:up-for-grabs

Metriche repository

Star
 (5644 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Description

Opacity is not updated properly when using the Custom Button. Check the provided code snippet

Steps to Reproduce

1.Run the sample with the provided code snippet 2.Click the button 3.Again click the button

Expected Behavior

Opacity should be updated properly as like normal Button

Actual Behavior

Opacity doesn’t update properly

Code snippet

  <local:ExtendedButton x:Name="xamarinbtn" Text="Xamarin Button" Opacity="1" BackgroundColor="Red" />
<Button Text="Xamarin Button" x:Name="xamarinbtn2"  BackgroundColor="Red"/>

           

            <Button Text="change" Clicked="Handle_Clicked" /> 

void Handle_Clicked(object sender, System.EventArgs e)

        {
     xamarinbtn.IsEnabled = !xamarinbtn.IsEnabled;

            xamarinbtn2.IsEnabled = !xamarinbtn2.IsEnabled;

            if (xamarinbtn.IsEnabled)

            {

                xamarinbtn.Opacity = 1;

      xamarinbtn2.Opacity = 1;

            }

            else

            {

                xamarinbtn.Opacity = 0.5;

                xamarinbtn2.Opacity = 0.5;

            }
}

public class ExtendedButton : Button

    {

public Setter DisabledBackgroundSetter { get; set; }

  public ExtendedButton()

        { var visualStateGroupList = new VisualStateGroupList();

            var visualStateGroup = new VisualStateGroup();

            var disabledState = new VisualState();

            disabledState.Name = "Disabled";

            DisabledBackgroundSetter = new Setter()

            {

                Property = Button.BackgroundColorProperty,

                Value = Color.Transparent

            };

            disabledState.Setters.Add(DisabledBackgroundSetter);

            disabledState.Setters.Add(new Setter()

            {

                Property = Button.BorderColorProperty,

                Value = Color.Transparent

            });

            disabledState.Setters.Add(new Setter()

            {

                Property = Button.OpacityProperty,

                Value = 0.5

            });

 

            visualStateGroup.States.Add(disabledState);

 

            visualStateGroupList.Add(visualStateGroup);

 

            VisualStateManager.SetVisualStateGroups(this, visualStateGroupList);

 

            BorderWidth = 1;

            CornerRadius = 4;

            BackgroundColor = Color.Transparent;

   }

   }

Guida contributor