question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Pressed" Visual State not changing back to "Normal" until you click away on Windows

See original GitHub issue

Description

I started a new MAUI app to see how to change a button’s visual state when it has been pressed. I found that the code I used for this works on macOS but not on Windows.

Firstly, I added colours to the Colors.xaml file

<Color x:Key="PrimaryBackgroundColour">#337ab7</Color>
<Color x:Key="PrimaryBorderColour">#2e6da4</Color>
<Color x:Key="PrimaryBackgroundActiveColour">#204d74</Color>
<Color x:Key="PrimaryBorderActiveColour">#122b40</Color>

I then added a new style to the Styles.xaml file which adds the Visual States

<Style x:Key="PrimaryButton"  
        TargetType="Button">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource PrimaryBackgroundColour}, Dark={StaticResource White}}" />
                        <Setter Property="BorderColor" Value="{AppThemeBinding Light={StaticResource PrimaryBorderColour}, Dark={StaticResource White}}" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Pressed">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource PrimaryBackgroundActiveColour}, Dark={StaticResource White}}" />
                        <Setter Property="BorderColor" Value="{AppThemeBinding Light={StaticResource PrimaryBorderActiveColour}, Dark={StaticResource White}}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

I then changed by Button to have this new style:

<Button 
    Text="Login"
    WidthRequest="300"
    HorizontalOptions="Center"
    Style="{StaticResource PrimaryButton}" />

When I ran the app on Windows I found that the button’s Visual State when pressed did change correctly, but it didn’t revert back to the Normal state until I clicked away (for example, clicking in to one of the Entry controls): Example on Windows

When I run the same code on macOS it works as expected: Example of macOS

Steps to Reproduce

I create a new Maui app.

I added colours to the Colors.xaml file

<Color x:Key="PrimaryBackgroundColour">#337ab7</Color>
<Color x:Key="PrimaryBorderColour">#2e6da4</Color>
<Color x:Key="PrimaryBackgroundActiveColour">#204d74</Color>
<Color x:Key="PrimaryBorderActiveColour">#122b40</Color>

I then added a new style to the Styles.xaml file which adds the Visual States

<Style x:Key="PrimaryButton"  
        TargetType="Button">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource PrimaryBackgroundColour}, Dark={StaticResource White}}" />
                        <Setter Property="BorderColor" Value="{AppThemeBinding Light={StaticResource PrimaryBorderColour}, Dark={StaticResource White}}" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Pressed">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource PrimaryBackgroundActiveColour}, Dark={StaticResource White}}" />
                        <Setter Property="BorderColor" Value="{AppThemeBinding Light={StaticResource PrimaryBorderActiveColour}, Dark={StaticResource White}}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

I then changed by Button to have this new style:

<Button 
    Text="Login"
    WidthRequest="300"
    HorizontalOptions="Center"
    Style="{StaticResource PrimaryButton}" />

Version with bug

6.0 Release Candidate 3

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows SDK 10.0.19041.0

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
blanc99commented, Aug 31, 2022

Able to reproduce as well. Thanks to jsuarezruiz, the workaround I put in is to add the ‘Focused’ visual state and set the colors to be the same as those of ‘Normal’ (as I do not need to highlight a focused button.)

1reaction
DarkNoircommented, Jun 5, 2022

Same issue on Windows 11 : 10.0.22000.0

More Info: Work Around, add event to reset. C# :

 private void Button_Pressed(object sender, EventArgs e)
{
    Button b = (Button)sender;
    Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(400), () =>
    {
        VisualStateManager.GoToState(b, "Normal");
    });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Button VisualState Focused not working
After click I want the color to remain applied until another button is clicked, then the text in the new button clicked will...
Read more >
Visual states - .NET MAUI
The .NET MAUI visual state manager can be used to make changes to XAML elements based on visual states set from code.
Read more >
Xamarin.Forms Visual State Manager
Use the Visual State Manager to make changes to XAML elements based on visual states set from code.
Read more >
Chapter 7: Customizing Narrator
This chapter outlines how to customize Narrator so it works best for you. This includes changing what voice and characteristics of the voice...
Read more >
VisualState Class (System.Windows)
Represents the visual appearance of the control when it is in a specific state. ... The Normal state is the state the button...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found