4 New Features You Must Try in Xamarin.Forms 4.5 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (166).NET Core  (29).NET MAUI  (112)Angular  (92)ASP.NET  (51)ASP.NET Core  (81)ASP.NET MVC  (89)Azure  (39)Blazor  (174)BoldSign  (2)DocIO  (23)Essential JS 2  (95)Essential Studio  (186)File Formats  (39)Flutter  (124)JavaScript  (210)Microsoft  (116)PDF  (71)React  (83)Succinctly series  (130)Syncfusion  (680)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (41)Windows Forms  (59)WinUI  (59)WPF  (142)Xamarin  (158)XlsIO  (27)Other CategoriesBarcode  (5)BI  (29)Bold BI  (6)Build conference  (7)Business intelligence  (54)Button  (4)C#  (126)Chart  (70)Cloud  (13)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (7)DataGrid  (59)Development  (487)Doc  (7)DockingManager  (1)eBook  (98)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (25)Extensions  (21)File Manager  (5)Gantt  (16)Gauge  (11)Git  (5)Grid  (28)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (374)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (34)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (10)Road Map  (11)Scheduler  (46)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (25)Solution Services  (3)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (107)UI  (263)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (24)Visual Studio Code  (17)Web  (460)What's new  (244)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
4 New features in Xamarin 4.5

4 New Features You Must Try in Xamarin.Forms 4.5

In each new release, Xamarin always has some exciting new features for us. The Xamarin.Forms 4.5 release, too, contains a lot of improvements and some exciting new features.

In this blog post, I will take you through the features of Xamarin.Forms 4.5 that excited me. I recommend that you try these features:

  • MediaElement (preview)
  • VSM TargetName
  • The stable release of AndroidX
  • State triggers (Preview)

MediaElement (preview)

A long waits is over with the availability of the new MediaElement control. Finally, Xamarin.Forms introduces MediaElement, a video and audio player interface used to play media.

The various sources supported by the MediaElement include the web, using a URI (HTTP or HTTPS); a resource embedded in the platform application, using the ms-appx:/// URI scheme; files that come from the app’s local and temporary data folders, using the ms-appdata:/// URI scheme; and the device’s library.

Its feature set includes aspect, auto play, buffering progress, duration, looping, keep screen on, position, and  volume. All these features are really amazing. Using these features, I just created my custom player with these options: video position, full screen, looping, play/pause, stop, and volume adjustment.

Let me describe the APIs I used to create these options in detail.

View: I used MediaElement, Button, and Slider to create my player UI.

       <ColumnDefinition Width="*"/> <ColumnDefinition Width="50"/>
      <MediaElement x:Name="videoPlayer" Aspect="AspectFit" Source="ms-appx:///Assets/BestXamarinUIControlswithIncrediblePerformance.mp4" ShowsPlaybackControls="False"/>
      <Slider x:Name="videoProgressSlider" VerticalOptions="End" Minimum="0" Maximum="100" ValueChanged="videoProgressSlider_ValueChanged"/>
      <Grid Grid.Column="1" >
          <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/>
      <Button Grid.Row="0" BackgroundColor="White" FontFamily="SEGMDL2.TTF#Segoe MDL2 Assets" Text="" Clicked="Button_Clicked"/>
      <Button Grid.Row="1" BackgroundColor="White" Text="" FontFamily="SEGMDL2.TTF#Segoe MDL2 Assets" Clicked="Button_Clicked_1"/>
      <Button Grid.Row="2" BackgroundColor="White" Text="" FontFamily="SEGMDL2.TTF#Segoe MDL2 Assets" Clicked="Button_Clicked_2"/>
      <Button Grid.Row="3" BackgroundColor="White" Text="" FontFamily="SEGMDL2.TTF#Segoe MDL2 Assets" Clicked="Button_Clicked_3"/>
      <Button Grid.Row="4" BackgroundColor="White" Text=""  FontFamily="SEGMDL2.TTF#Segoe MDL2 Assets"  Clicked="Button_Clicked_4"/>

Video position:  I used the Position property of type TimeSpan to describe the current progress through the media’s playback time. As seen in the following code, I calculated the percentage value based on the current value of the slider.

private void videoProgressSlider_ValueChanged(object sender, ValueChangedEventArgs e)
   var duration = this.videoPlayer.Duration.Value.Ticks;
   var slideValue = (sender as Slider).Value;
   var durationPercentage = duration / 100;
   var tickValue = durationPercentage * slideValue;
   var timeSpan = new TimeSpan((long) tickValue);
   this.videoPlayer.Position = timeSpan;

Full screen: The Aspect property of MediaElement of type Aspect determines how the media will be scaled to fit the display area. You can make the video fill the screen using this option.

this.videoPlayer.Aspect = Aspect.Fill;

Play/pause and stop: MediaElement has built-in methods for these: Play(), Pause() and Stop().

Volume: Using the API Volume of type double, the media’s volume can be adjusted. The volume is represented on a linear scale between 0 and 1.

this.videoPlayer.Volume = 0.5;


VSM TargetName saves you from repeating code

In a classic approach to updating a property of a control based on its state, we may look for direct properties to set values in different states, or we could watch for a change in the state of the control to update it in the code-behind. VisualStateManager (VSM) makes it easier for developers to specify the appearance of a control depending on its visual state.

Now, VisualStateManager can target elements (in the visual tree) to set properties on any control within the same visual tree. This will help set the state on multiple controls. For example, I have a reset password page that contains a DataGrid with two Entries and a Button with the strength indicator views weak, medium, and strong. In a classical approach, I need to write a lot of code to set the state for each view. But now, with Xamarin.Forms 4.5, this is made simpler by setting the target element in a single element state.

<Grid x:Name="MainGrid" WidthRequest="200" HeightRequest="250" Padding="10" VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout VerticalOptions="Center" Orientation="Vertical">
    <Entry  Placeholder="Current Password" IsPassword="True"/>
    <Frame x:Name="PasswordBorder" Padding="2" CornerRadius="0">
        <Entry Placeholder="New Password" TextChanged="PasswordEntry_TextChanged" IsPassword="True" >
                <VisualStateGroup Name="PasswordStrength">
                    <VisualState Name="WeakPassword">
                            <Setter TargetName="PasswordBorder"  Property="Frame.BorderColor" Value="#ad1400"/>
                            <Setter TargetName="WeakGrid" Property="Grid.BackgroundColor" Value="#ffb4ab"/>
                            <Setter TargetName="MediumGrid" Property="Grid.BackgroundColor" Value="#00ffffff"/>
                            <Setter TargetName="StrongGrid" Property="Grid.BackgroundColor" Value="#00ffffff"/>
                            <Setter TargetName="StrengthLabel" Property="Label.Text" Value="Weak"/>
                    <VisualState Name="MediumPassword">
                            <Setter TargetName="PasswordBorder"  Property="Frame.BorderColor" Value="#ad8a00"/>
                            <Setter TargetName="WeakGrid" Property="Grid.BackgroundColor" Value="#fcff9e"/>
                            <Setter TargetName="MediumGrid" Property="Grid.BackgroundColor" Value="#fcff9e"/>
                            <Setter TargetName="StrongGrid" Property="Grid.BackgroundColor" Value="#00ffffff"/>
                            <Setter TargetName="StrengthLabel" Property="Label.Text" Value="Medium"/>
                    <VisualState Name="StrongPassword">
                            <Setter TargetName="PasswordBorder"  Property="Frame.BorderColor" Value="#02a60f"/>
                            <Setter TargetName="WeakGrid" Property="Grid.BackgroundColor" Value="#9effb8"/>
                            <Setter TargetName="MediumGrid" Property="Grid.BackgroundColor" Value="#9effb8"/>
                            <Setter TargetName="StrongGrid" Property="Grid.BackgroundColor" Value="#9effb8"/>
                            <Setter TargetName="StrengthLabel" Property="Label.Text" Value="Strong"/>
        <Grid HeightRequest="20" ColumnSpacing="0" RowSpacing="0">
            <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions>
            <Grid x:Name="WeakGrid" Grid.Column="0" ColumnSpacing="0" RowSpacing="0"/>
            <Grid x:Name="MediumGrid" Grid.Column="1" ColumnSpacing="0" RowSpacing="0"/>
            <Grid x:Name="StrongGrid" Grid.Column="2" ColumnSpacing="0" RowSpacing="0"/>
        <Label x:Name="StrengthLabel" VerticalOptions="Center" HorizontalOptions="Center" />
    <Entry Placeholder="Confirm New Password" IsPassword="True"/>
    <Button Text="Reset Password" TextColor="White"/>

I set entry states “WeakPassword,” “MediumPassword,” and “StrongPassword” through Visual State Manager based on text length.

private void PasswordEntry_TextChanged(object sender, TextChangedEventArgs e)
    var entryObject = sender as Entry;
    if (entryObject.Text.Length < 8)
        VisualStateManager.GoToState(entryObject, "WeakPassword");
    else if (entryObject.Text.Length < 12)
        VisualStateManager.GoToState(entryObject, "MediumPassword");
    else if (entryObject.Text.Length > 12)
        VisualStateManager.GoToState(entryObject, "StrongPassword");

Example of VSM using TargetName
Example of VSM using TargetName

The stable release of AndroidX

AndroidX packages fully replace the Support Library, so now the original Android Support Library is no longer maintained. Based on this update, Xamarin.Forms is also migrated to AndroidX from the original Android Support Library. So, it’s time to migrate your existing apps to AndroidX to make your app compatible with Android 10. Updating to AndroidX libraries makes it easier for developers to consume Android packages. Hopefully, it will provide several advantages in Xamarin.Forms and Xamarin.Essential in upcoming releases.

Are you planning to migrate your existing app? Then, this blog will be helpful for you.

State triggers (preview)

Triggers allow you to change the appearance of controls based on events or property changes. Already, Xamarin.Forms has property triggers, data triggers, event triggers, and multi-triggers. With  Xamarin.Forms 4.5, you can enjoy additional state triggers: adaptive trigger , compare state trigger, device state trigger and orientation trigger.

Adaptive trigger

A VisualState change can be triggered when the window is specified with a height or width.

<VisualState x:Name="Vertical">
          <AdaptiveTrigger MinWindowWidth="0" />
          <Setter Property="Orientation"
          Value="Vertical" />

Compare state trigger

A VisualState change can be triggered when a property is equal to a specific value.

<VisualState x:Name="Checked">
         <CompareStateTrigger Property="{Binding Source={x:Reference switch}, Path=IsToggled}"
                              Value="True" />
        <Setter Property="BackgroundColor"
                Value="Blue" />

Device trigger

A VisualState change can be triggered based on the device platform on which the app is running.

<VisualState x:Name="iOS">
     <DeviceStateTrigger Device="iOS" />
     <Setter Property="BackgroundColor"
             Value="Orange" />

Orientation trigger

A VisualState change can be triggered when the orientation of the device changes.

<VisualState x:Name="Landscape">
          <OrientationStateTrigger Orientation="Landscape" />
     <Setter Property="BackgroundColor"
             Value="Orange" />

Other interesting features

The Xamarin.Forms 4.5 update is not limited to these exciting features. You can check out other interesting features and enhancement in Xamarin.Forms 4.5 in the release notes.


I hope you, too, are excited about these new features available with Xamarin.Forms 4.5. So, why don’t you try them now? Start by downloading Xamarin.Forms 4.5 in NuGet. Don’t forget to set flags for preview controls and to try our Syncfusion Xamarin.Forms controls, which are compatible with Xamarin.Forms 4.5. I am sure you will definitely enjoy this combo of Xamarin.Forms 4.5 and Syncfusion Xamarin.Forms controls. You can also explore our AndroidiOS and UWP demo apps!

If you come across any issues using our controls with 4.5, please contact us through our support forumDirect-Trac, or feedback portal. We are happy to assist you.

If you liked this blog post, we think you’ll also enjoy:

[Blog] How to Use Xamarin.Forms Visual State Manager

[Blog] Easily Animate Your Xamarin Applications with Lottie Animations

[Blog] 7 Ways to Boost Your Xamarin Development Productivity

[Ebook] Xamarin.Forms Succinctly


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top