How to Use Xamarin.Forms Visual State Manager | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Visual State Manager

How to Use Xamarin.Forms Visual State Manager

If you are a hardcore XAML fan like me, you probably know what a Visual State Manager (VSM) is. Like me, you might have been heartbroken when you learned that Xamarin.Forms didn’t support VSM in its early versions. For those who don’t know what VSM is, let me explain. VSM makes it easier for the developer to specify the appearance of a control depending on its visual state.

In a classic approach to updating a property of a control based on its state, we may look for direct properties to set the value in different states, or we could watch for a change in the state of the control to update it in the code behind. In Xamarin.Forms, there was no direct way to set a property to the control based on its state. Well, I was glad when Xamarin.Forms 3.0 was released with support for a Visual State Manager.

The Visual State Manager (VSM) in Xamarin.Forms provides CommonStates, a visual state group with three visual states:

  • Normal
  • Disabled
  • Focused

For all classes obtained from VisualElement, which is the base class for View and Page, this visual state group is endorsed.

The common states

The Visual State Manager allows you to include in your XAML file parts that can change the visual appearance of a view if the view is in normal, disabled, or focused states. These are called the common states.

For example, let’s consider an Entry control, and update the background color to red when it is disabled. The following XAML code will do this:

<Entry FontSize=”18”>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name=”CommonStates”>
            <VisualState x:Name=”Disabled”>
                <VisualState.Setters>
                    <Setter Property=”BackgroundColor” Value=”Red” />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Entry>

VSM works not just with the common states, but also with custom states. Let’s proceed with an example of customizing the text property of the Syncfusion Button control in various states.

Customizing Syncfusion Button with VSM

The Syncfusion Button has the following states:

  • Normal
  • Pressed
  • Disabled
  • Checked (toggle button)

In this example, we will customize the Text property of the Button control with respect to these visual states. Please follow the below steps:

  1. Including the Button control.
  2. Declaring visual state groups.
  3. Adding visual state groups.
  4. Adding the visual states in the group.
  5. Customizing the Text property.

Step 1: Including the Button control

First, add a reference to the Syncfusion Button to the project. Then, add the XML namespace and Button control to the main page.

xmlns:button="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"

<button:SfButton>
</ button:SfButton>

Step 2: Declaring visual state groups

VisualStateGroups is a bindable property of type VisualStateGroupList, which is a collection of objects of VisualStateGroup. VisualStateManager defines it.

Insert VisualStateGroups between the button tags.

<button:SfButton>
            <VisualStateManager.VisualStateGroups>

            </VisualStateManager.VisualStateGroups>
</ button:SfButton>

Step 3: Adding visual state groups

The VisualStateGroups property can hold more than the VisualStateGroup, but here we are going to add a single group to hold the common states of the Syncfusion Button control.

<button:SfButton>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">

                </VisualStateGroup>
                <VisualStateGroup x:Name="ButtonStates">

                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
</ button:SfButton>

Step 4: Adding visual states in the groups

The VisualStateGroup defines a property named States, which is a collection of VisualState. Add a pair of tags for every state of the Syncfusion Button control.

<button:SfButton>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal">

            </VisualState>
            <VisualState x:Name="Disabled">

            </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="ButtonStates">
            <VisualState x:Name="Pressed">

            </VisualState>
            <VisualState x:Name="Checked">

            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</button:SfButton>

Step 5: Customizing the text property

VisualState has a collection of Setter objects, defined under a property named Setters. Define the setter object in Setters to change the Text property of the Button control based on its state.

<button:SfButton>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal">
                <VisualState.Setters>
                    <Setter Property="Text" Value="Enabled" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Disabled">
                <VisualState.Setters>
                    <Setter Property="Text" Value="Disabled" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="ButtonStates">
            <VisualState x:Name="Pressed">
                <VisualState.Setters>
                    <Setter Property="Text" Value="Clicked" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Checked">
                <VisualState.Setters>
                    <Setter Property="Text" Value="Checked" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</button:SfButton>

Syncfusion Button with customized text using Xamarin.Forms Visual State Manager

Syncfusion Button with Customized Text Using VSM

Conclusion

Since the introduction of VSM in Xamarin.Forms 3.0, I have been playing around with VSM. One use-case I find interesting is using it in an adaptive layout to update the state with respect to the orientation, shifting between portrait and landscape. I hope you find it equally interesting and play around with it yourself. Let me know your thoughts as comments below.

In this blog, we have studied how to customize properties of Xamarin.Forms controls based on their states using the Visual State Manager. We have seen how to customize the properties based on both the common states and custom states of the control. The sample for customizing the Text property of the Syncfusion Button control based on its states using the Visual State Manager can be checked out from this GitHub location.

Reference: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual-state-manager

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed