Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

Simplifying Visual State Manager with TargetName


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

simplifying_visual

Table of Content

What is Visual State Manager?

The Visual State Manager (VSM) is a structured way to make some changes on the controls to the user interface from code, and the visual state manager is the concept of visual states. All the user interface is defined in XAML, so we can include visual state manager effects on the XAML. We can apply visual state manager effects on all the XAML controls to make unique controls. Let’s took an example of a Button, a button has different visual appearances like a button pressed, whether it’s disabled, or has input focus.

“CommonStates” is the visual state group named that is defined on the Visual State Manager. They have the following visual states:

  1. “Normal”
  2. “Selected”
  3. “Focused”
  4. “Disabled”

VisualElement is the base class for Page and View, and the visual state group only support the class those derived from VisualElement.

Triggers and visual state manager both are the same but the difference is, using trigger we can also change the Ui based on the events and property. But in visual state manager we can change the Ui based on states it is easy to use and their structure is less confusing.

Common States

In XAML, if the view is disabled, or normal, or has the input focus these are known as the common states.
 


            
                
                    

                    

                    

                    

                    

                    
                
            

This is just an example of how we can use visual state manager. Visual state group is a common group state on that we can include pair of visual state. After that, we can define a setter property on the visual state. You can use the setter property with the visual state and inside the VisualState.Setter like the below,
 


            
                
                    
                        
                            
                        
                    

                    
                        
                            
                        
                    

                    
                        
                            
                        
                    
                
            

Now, we can see a simple Entry example using a visual state manager.
 


    
        
            
                

                    
                        
                            
                            
                            
                        
                    

                    
                        
                            
                            
                            
                            
                        
                    

                    
                        
                            
                        
                    
                
            
        
    

Here we can add Entry control with the visual state group and VisualState.Setter, inside VisualState.Setter we can add some other setter property to set the entry’s background color, font size, text color, and height.

   

In this example, we have created an entry for example when we enter some data they will change the entry’s height and color, and when we remove focus it will change again.

Output

ai-Hiring-banner

Image: Entry with Visual State Manager

ai-Hiring-banner

Image: Changed Entry Control

ai-Hiring-banner

Image: Effect after mouse over

We can create an example with one entry control now and can create an example with three entry control with some conditions.
 




    
            
            

            

In this example, we can add three entry controls first entry control will be normal entry control, the second entry control will be disabled when we type something on the third entry control then the second entry control will be enabled and we can remove the text from the third entry control the second entry control will be disabled.

Output

ai-Hiring-banner

Image: Multiple Entry Controls with Visual State Manager

ai-Hiring-banner

Image: Entry control with some condition

ai-Hiring-banner

Image: Enabled second entry control

After this example, we have created one more example with some conditions using the code-behind.

 

Looking for the Best Custom Software Development Company ?

Your Search ends here.


MainPage.Xaml



        
            
                
                    
                        
                            
                            
                        
                    
                    
                        
                            
                            
                        
                    
                
            
            

Mainpage.Xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace VisualState
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Validation(false);
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            bool isValid = Regex.IsMatch(e.NewTextValue, @"^[2-9]\d{2}-\d{3}-\d{4}$");
            Validation(isValid);
        }

        void Validation(bool isValid)
        {
            string VSM = isValid ? "Valid" : "Invalid";
            VisualStateManager.GoToState(stackLayout, VSM);
        }
    }
}
 

In this example, we can set button visibility using visual state manager when the user doesn’t enter a valid number format the button will be disabled and user entered a valid number the button will be enabled.

Output

ai-Hiring-banner

Image: Submit button disable

ai-Hiring-banner

Image: Can’t start with 0 or 1

ai-Hiring-banner

Image: Submit button enabled

Conclusion

In this blog, we have learned the visual state manager, which is a structured way to design control. Suppose we want to increase the height of an entry control when we type something on that. And we can also set the text color or entry color based on the conditions. Also, do that, change the color of entry control when control is focused.

Simplifying Visual State Manager with TargetName

simplifying_visual

Table of Content

What is Visual State Manager?

The Visual State Manager (VSM) is a structured way to make some changes on the controls to the user interface from code, and the visual state manager is the concept of visual states. All the user interface is defined in XAML, so we can include visual state manager effects on the XAML. We can apply visual state manager effects on all the XAML controls to make unique controls. Let’s took an example of a Button, a button has different visual appearances like a button pressed, whether it’s disabled, or has input focus.

“CommonStates” is the visual state group named that is defined on the Visual State Manager. They have the following visual states:

  1. “Normal”
  2. “Selected”
  3. “Focused”
  4. “Disabled”

VisualElement is the base class for Page and View, and the visual state group only support the class those derived from VisualElement.

Triggers and visual state manager both are the same but the difference is, using trigger we can also change the Ui based on the events and property. But in visual state manager we can change the Ui based on states it is easy to use and their structure is less confusing.

Common States

In XAML, if the view is disabled, or normal, or has the input focus these are known as the common states.
 


            
                
                    

                    

                    

                    

                    

                    
                
            

This is just an example of how we can use visual state manager. Visual state group is a common group state on that we can include pair of visual state. After that, we can define a setter property on the visual state. You can use the setter property with the visual state and inside the VisualState.Setter like the below,
 


            
                
                    
                        
                            
                        
                    

                    
                        
                            
                        
                    

                    
                        
                            
                        
                    
                
            

Now, we can see a simple Entry example using a visual state manager.
 


    
        
            
                

                    
                        
                            
                            
                            
                        
                    

                    
                        
                            
                            
                            
                            
                        
                    

                    
                        
                            
                        
                    
                
            
        
    

Here we can add Entry control with the visual state group and VisualState.Setter, inside VisualState.Setter we can add some other setter property to set the entry’s background color, font size, text color, and height.

   

In this example, we have created an entry for example when we enter some data they will change the entry’s height and color, and when we remove focus it will change again.

Output

ai-Hiring-banner

Image: Entry with Visual State Manager

ai-Hiring-banner

Image: Changed Entry Control

ai-Hiring-banner

Image: Effect after mouse over

We can create an example with one entry control now and can create an example with three entry control with some conditions.
 




    
            
            

            

In this example, we can add three entry controls first entry control will be normal entry control, the second entry control will be disabled when we type something on the third entry control then the second entry control will be enabled and we can remove the text from the third entry control the second entry control will be disabled.

Output

ai-Hiring-banner

Image: Multiple Entry Controls with Visual State Manager

ai-Hiring-banner

Image: Entry control with some condition

ai-Hiring-banner

Image: Enabled second entry control

After this example, we have created one more example with some conditions using the code-behind.

 

Looking for the Best Custom Software Development Company ?

Your Search ends here.


MainPage.Xaml



        
            
                
                    
                        
                            
                            
                        
                    
                    
                        
                            
                            
                        
                    
                
            
            

Mainpage.Xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace VisualState
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Validation(false);
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            bool isValid = Regex.IsMatch(e.NewTextValue, @"^[2-9]\d{2}-\d{3}-\d{4}$");
            Validation(isValid);
        }

        void Validation(bool isValid)
        {
            string VSM = isValid ? "Valid" : "Invalid";
            VisualStateManager.GoToState(stackLayout, VSM);
        }
    }
}
 

In this example, we can set button visibility using visual state manager when the user doesn’t enter a valid number format the button will be disabled and user entered a valid number the button will be enabled.

Output

ai-Hiring-banner

Image: Submit button disable

ai-Hiring-banner

Image: Can’t start with 0 or 1

ai-Hiring-banner

Image: Submit button enabled

Conclusion

In this blog, we have learned the visual state manager, which is a structured way to design control. Suppose we want to increase the height of an entry control when we type something on that. And we can also set the text color or entry color based on the conditions. Also, do that, change the color of entry control when control is focused.