4 New Features You Must Try in Xamarin.Forms 4.5 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)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  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)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  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)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.

<Grid>
  <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*"/> <ColumnDefinition Width="50"/>
  </Grid.ColumnDefinitions>
      <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" >
       <Grid.RowDefinitions>
          <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/>
       </Grid.RowDefinitions>
      <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"/>
  </Grid>
</Grid>

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" >
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup Name="PasswordStrength">
                    <VisualState Name="WeakPassword">
                        <VisualState.Setters>
                            <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.Setters>
                    </VisualState>
                    <VisualState Name="MediumPassword">
                        <VisualState.Setters>
                            <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.Setters>
                    </VisualState>
                    <VisualState Name="StrongPassword">
                        <VisualState.Setters>
                            <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"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Entry>
    </Frame>
    <Grid>
        <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"/>
        </Grid>
        <Label x:Name="StrengthLabel" VerticalOptions="Center" HorizontalOptions="Center" />
    </Grid>
    <Entry Placeholder="Confirm New Password" IsPassword="True"/>
    <Button Text="Reset Password" TextColor="White"/>
</StackLayout>
</Grid>

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">
     <VisualState.StateTriggers>
          <AdaptiveTrigger MinWindowWidth="0" />
     </VisualState.StateTriggers>
     <VisualState.Setters>
          <Setter Property="Orientation"
          Value="Vertical" />
     </VisualState.Setters>
</VisualState>

Compare state trigger

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

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

Device trigger

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

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

Orientation trigger

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

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

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.

Conclusion

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

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed