locked
Xamarin.Forms - Target UWP - Custom Button Style Color onPressed, onHover RRS feed

  • Question

  • User5612 posted

    Hi there !

    I'm trying to customize color of my buttons for my Xamarin.Forms project (main target is uwp).

    The basic color is grey with differents shade of grey when hover and pressed.

    I'm trying to do the same with different shades of colors... but the pressed style don't apply...

    <Setter Property="VisualStateManager.VisualStateGroups">
                      <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="PointerOver">
                          <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="{StaticResource LightBlue}"/>
                            <Setter Property="TextColor" Value="{StaticResource White}" />
                          </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Normal">
                          <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="{StaticResource DarkBlue}" />
                            <Setter Property="TextColor" Value="{StaticResource White}" />
                          </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Pressed">
                          <VisualState.Setters>
                            <Setter Property="TextColor" Value="{StaticResource White}" />
                            <Setter Property="Scale" Value="0.8" />
                            <Setter Property="BackgroundColor" Value="{StaticResource LightBlue2}" />
                          </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                          <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="{DynamicResource LightGray}" />
                          </VisualState.Setters>
                        </VisualState>
                      </VisualStateGroup>
                  </Setter>
    

    I tried a lot of things on the internet but i can't find a correct solution. I'm trying to stay in the ResourceDictionary to apply style as it's a SIMPLE solution. Is it possible ? Because i'm a bit pissed by the fact that having different button COLOR STYLE is SO difficult or tricky...

    Thanks for your help :)

    Thursday, June 13, 2019 4:04 PM

All replies

  • User382871 posted

    Create a Custom_Renderer. Then listen for the event and set the Button style. ``` public class CustomButtonRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); var button = (RoundedButton)e.NewElement; if (Control != null) { button.SizeChanged += OnSizeChanged; }

    }
    private void OnSizeChanged(object sender, EventArgs e)
    {
        var button = (RoundedButton)sender;
        Control.Style = (Style)Application.Current.Resources["RoundedButtonStyle"];
        button.SizeChanged -= OnSizeChanged;
    }
    

    } ```

    See the link: https://forums.xamarin.com/discussion/comment/187755/#Comment_187755

    Friday, June 14, 2019 8:50 AM