Introduction
Hello guys...
Today I would like to show you about Xamarin Forms 4.6. In this update, there are lots of cool new features.
So, let's get started.
Implementation
First, we start the VS 2019 and click on create a new project.
Then search the Xamarin.forms project and select Mobile App(Xamarin.Forms). Click Next.
Now, I am going to the project name which is DemoXF4_6, then click the create button.
After clicking the create button, a dialogue appears like in the below image.
This is another dialogue window where you can:
- Choose the blank app
- Select the project(Android, iOS) you want to create
- Select the Code Sharing Strategy .NET Standard
- Click OK, then create the project
First, we check the xamarin.forms latest Nuget Package. Check if the xamarin forms Nuget Package is updated or not. If not, we will update it. The following are the steps for updating the package.
First, we select the solution and right-click on the solution and then select the Nuget package as shown in this below screen.
Now a new window appears (Nuget - Solution). This window contains the information to all packages used in this project. Here, we can select the install tab, and search the xamarin.Forms and update it in PCL, Android, and iOS.
Feature of Xamarin Forms 4.6
- Fonts
- GIF Supported
- RadioButton
- Expender
- VisualMaterial
- AppTheme
Before using this feature, we add the flag in App.cs such as:
- Device.SetFlags(new string[]
- { "RadioButton_Experimental",
- "AppTheme_Experimental",
- "Markup_Experimental",
- "Expander_Experimental"
- });
Font in Xamarin Forms
Setup
First, we create a Fonts Folder and add a font like PressStart2P-Regular.ttf or materialdesignicons-webfont.ttf. After adding the fonts, we can set the font property None to an Embedded resource, and set the reference or aliased in assemblyInfo file and write a line for set the Alias and use it in our controls.
- [assembly: ExportFont("PressStart2P-Regular.ttf", Alias ="FontCode")]
- [assembly: ExportFont("materialdesignicons-webfont.ttf",Alias ="FontAwesome")]
Example
- <Label
- FontFamily="FontCode"
- FontSize="Large"
- HorizontalOptions="Center"
- Text="Login Form"
- VerticalOptions="Center" />
GIF Supported in Xamarin Forms
GIF image is simple image animation, simply put, it is a 5-10 sec video clip animation. This is a very easy part to load into the image. We can add the IsAnimationPlaying as True.
Example
- <Image
- Aspect="Fill"
- HeightRequest="250"
- IsAnimationPlaying="True"
- Source="https://steamuserimages-a.akamaihd.net/ugc/931561575357966166/6BE65F9887FD3207D44464189B1DF1A9A3ACEDD9/" />
RadioButton in Xamarin Forms
RadioButton, which is the most required control on Camarin forms, is a type of button control that allows the user to select one option in the group. We can define the radio group using the GroupName Property.
Example
- <RadioButton
- GroupName="Remember"
- IsChecked="False"
- Text="Remember" />
-
- <RadioButton
- GroupName="Remember"
- IsChecked="False"
- Text="Not Remember" />
Expender in Xamarin Forms
In Xamarin Forms, the expander is a Collapsible container. In this control, one is Header container that is shown and another which is a hidden part is the expander container. When users tap on the header, another part is collapsed.
Example
- <Expander
- x:Name="expander"
- Grid.Row="1"
- Grid.Column="0"
- Margin="0,0,0,10"
- Padding="0,20"
- FlexLayout.Basis="100%">
- <Expander.Header>
- <Frame
- Padding="5"
- BackgroundColor="AntiqueWhite"
- Visual="Material">
- <StackLayout
- Margin="0,0,0,6"
- HorizontalOptions="FillAndExpand"
- Orientation="Horizontal">
-
- <Image Source="{FontImage FontFamily=FontAwesome, Glyph={x:Static
- helper:IconFont.Notebook}, Size=30, Color=Black}" />
-
- <Label
- FontSize="18"
- HorizontalOptions="FillAndExpand"
- Text="Order Status" />
- </StackLayout>
- </Frame>
- </Expander.Header>
- <Grid
- Padding="10,0"
- BackgroundColor="AliceBlue"
- RowSpacing="0">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="10" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <Grid
- Grid.Row="0"
- Grid.Column="0"
- RowSpacing="0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <BoxView
- Grid.Row="0"
- Grid.Column="0"
- BackgroundColor="Accent"
- CornerRadius="10"
- HeightRequest="10"
- WidthRequest="10" />
- <BoxView
- Grid.Row="1"
- Grid.Column="0"
- BackgroundColor="Accent"
- HorizontalOptions="Center"
- VerticalOptions="Start"
- WidthRequest="2" />
- </Grid>
- <Label
- Grid.Row="0"
- Grid.Column="1"
- Text="Ordered and Approved" />
- <Grid
- Grid.Row="1"
- Grid.Column="0"
- RowSpacing="0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <BoxView
- Grid.Row="0"
- Grid.Column="0"
- BackgroundColor="Accent"
- CornerRadius="10"
- HeightRequest="10"
- WidthRequest="10" />
- <BoxView
- Grid.Row="1"
- Grid.Column="0"
- BackgroundColor="Accent"
- HorizontalOptions="Center"
- VerticalOptions="Start"
- WidthRequest="2" />
- </Grid>
- <Label
- Grid.Row="1"
- Grid.Column="1"
- Text="Packed" />
- <Grid
- Grid.Row="2"
- Grid.Column="0"
- RowSpacing="0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <BoxView
- Grid.Row="0"
- Grid.Column="0"
- BackgroundColor="Accent"
- CornerRadius="10"
- HeightRequest="10"
- WidthRequest="10" />
- <BoxView
- Grid.Row="1"
- Grid.Column="0"
- BackgroundColor="Accent"
- HorizontalOptions="Center"
- VerticalOptions="Start"
- WidthRequest="2" />
- </Grid>
- <Label
- Grid.Row="2"
- Grid.Column="1"
- Text="Shipped" />
- <Grid
- Grid.Row="3"
- Grid.Column="0"
- RowSpacing="0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <BoxView
- Grid.Row="0"
- Grid.Column="0"
- BackgroundColor="Accent"
- CornerRadius="10"
- HeightRequest="10"
- WidthRequest="10" />
- <BoxView
- Grid.Row="1"
- Grid.Column="0"
- BackgroundColor="Accent"
- HorizontalOptions="Center"
- VerticalOptions="Start"
- WidthRequest="2" />
- </Grid>
-
- <Label
- Grid.Row="3"
- Grid.Column="1"
- Text="Delivery" />
-
- <Grid
- Grid.Row="4"
- Grid.Column="0"
- RowSpacing="0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <BoxView
- Grid.Row="0"
- Grid.Column="0"
- BackgroundColor="Accent"
- CornerRadius="10"
- HeightRequest="10"
- WidthRequest="10" />
- <BoxView
- Grid.Row="1"
- Grid.Column="0"
- BackgroundColor="Accent"
- HorizontalOptions="Center"
- IsVisible="False"
- VerticalOptions="Start"
- WidthRequest="2" />
- </Grid>
-
- <Label
- Grid.Row="4"
- Grid.Column="1"
- Text="Installation & Demo" />
- </Grid>
- </Expander>
Visual.Material
Xamarin.Forms.Visual.Material is used to apply the material design rule in Xamarin Forms apps that look identical on iOS and Android.
If you want to use the material design in xamarin forms control so firstly we need to add Nuget Xamarin.Forms.Visual.Material package in all framework.
After adding Nuget Xamarin.Forms.Visual.Material package the most important thing is that we can initialize the control in Android and iOS.
Android MainActivity.cs,
- global::Xamarin.Forms.FormsMaterial.Init(this, savedInstanceState);
iOS AppDelegate.cs
- global::Xamarin.Forms.FormsMaterial.Init();
Example
- <Entry
- Placeholder="Username"
- Style="{StaticResource EntryStyle}"
- Visual="Material" />
-
- <Entry
- IsPassword="True"
- Placeholder="Password"
- Style="{StaticResource EntryStyle}"
- Visual="Material" />
AppTheme
Recently, our mobile device includes a Dark or Light Theme, AppTheme is another different API in xamarin.forms for using the dark or light mode. It immediately responds when the Device is changed to Dark or Light and uses it with Dynamically Like DynamicResources.
Example
- <AppThemeColor
- x:Key="PageBackgroundColor"
- Dark="#0B0C10"
- Light="#E7717D" />