Introduction
Every single app possesses a settings screen, facilitating several tiny preferences, open-source license information, program version information, Fingerprint authentication in the app, etc.
The string formatting covers the code-specific formatting of various types of objects. Along with the values of text that are being formatted, we can add other text also.
String formatting is very efficient when we are displaying strings in code format and the tool used for it is String. Format method and it’s a static method.
Occasionally, it is appropriate to display data of string format of any object or value using Data Bindings.
It is a facility that is forwarded into data bindings.
We set the StringFormat property of the Binding markup extension to a standard .NET standard which formats string with one placeholder.
<Slider x:Name=”sldr” />
<Label Text=”{Binding Source={x:Reference sldr}, Path=Value,
StringFormat=’The slider value is {0:F4}’}” />
<?xml version=”1.0″ encoding=”utf-8″ ?>
<ContentPage xmlns=”http://xamarin.com/schemas/2014/forms”
xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”
x:Class=”App1.StringFormatting”
xmlns:sys=”clr-namespace:System;assembly=netstandard”
Title=”String Formatting”>
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType=”Label”>
<Setter Property=”HorizontalTextAlignment” Value=”Center” />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Margin=”10″ BackgroundColor=”CadetBlue”>
<Label Text=”String Formatting in Slider Control” TextColor=”GreenYellow”
FontSize=”Subtitle”/>
<Slider x:Name=”sldr” />
<Label Text=”{Binding Source={x:Reference sldr}, Path=Value,
StringFormat=’The value in slider is {0:F4}’}” />
<Label Text=”String Formatting in Time Picker Control” TextColor=”GreenYellow” FontSize=”Subtitle”/>
<TimePicker x:Name=”timePckr” />
<Label Text=”{Binding Source={x:Reference timePckr},Path=Time,
StringFormat=’The TimeSpan is {0:c}’}” />
<Label Text=”String Formatting in Entry Control” TextColor=”GreenYellow” FontSize=”Subtitle”/>
<Entry x:Name=”entry” />
<Label Text=”{Binding Source={x:Reference entry},Path=Text,
StringFormat=’The text in the Control is "{0}"’}” />
<StackLayout BindingContext=”{x:Static sys:DateTime.Now}”>
<Label Text=”String Formatting in Label Control for Date” TextColor=”GreenYellow” FontSize=”Subtitle”/>
<Label Text=”{Binding}” />
<Label Text=”{Binding StringFormat=’The Long Date is {0:D}’}” />
</StackLayout>
<Label Text=”String Formatting in Label Control for Decimal No.” TextColor=”GreenYellow” FontSize=”Subtitle”/>
<StackLayout BindingContext=”{x:Static sys:Math.PI}”>
<Label Text=”{Binding}” />
<Label Text=”{Binding StringFormat=’PI to 4 decimal points = {0:F4}’}” />
<Label Text=”{Binding StringFormat=’PI in scientific notation = {0:E7}’}” />
</StackLayout>
</StackLayout>
</ContentPage>
<Slider x:Name=”sldr” />
<Label Text=”{Binding Source={x:Reference sldr}, Path=Value,
StringFormat=’The value in slider is {0:F4}’}” />
Image: Output for different String Formats
<?xml version=”1.0″ encoding=”utf-8″ ?>
<ContentPage xmlns=”http://xamarin.com/schemas/2014/forms”
xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”
x:Class=”App1.StringFormatting”
xmlns:sys=”clr-namespace:System;assembly=netstandard”>
<StackLayout Padding=”10, 0″>
<Label x:Name=”label”
Text=”Xamarin”
FontSize=”50″
HorizontalOptions=”Center”
VerticalOptions=”CenterAndExpand” />
<Slider x:Name=”sldr”
VerticalOptions=”CenterAndExpand”
Value=”{Binding Source={x:Reference label},
Path=Opacity}” />
<Label Text=”{Binding Source={x:Reference sldr}, Path=Value,
StringFormat=’The value in slider is {0:F4}’}”
HorizontalOptions=”Center”
VerticalOptions=”CenterAndExpand”
FontSize=”Body”/>
</StackLayout>
</ContentPage>
Image: Output for Opacity of Text
<?xml version=”1.0″ encoding=”utf-8″ ?>
<ContentPage xmlns=”http://xamarin.com/schemas/2014/forms”
xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”
xmlns:globe=”clr-namespace:System.Globalization;assembly=netstandard”
x:Class=”Blog.StringFormatting”>
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType=”Slider”>
<Setter Property=”VerticalOptions” Value=”CenterAndExpand” />
</Style>
<Style TargetType=”Label”>
<Setter Property=”HorizontalTextAlignment” Value=”Center” />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<StackLayout.BindingContext>
<local:HslColorViewModel Color=”Fuscia” />
</StackLayout.BindingContext>
<BoxView Color=”{Binding Color}”
VerticalOptions=”FillAndExpand” />
<StackLayout Margin=”10, 0″>
<Label Text=”{Binding Name}” />
<Slider Value=”{Binding Hue}” />
<Label Text=”{Binding Hue, StringFormat=’Hue = {0:F3}’}” />
<Slider Value=”{Binding Saturation}” />
<Label Text=”{Binding Saturation, StringFormat=’Saturation = {0:F3}’}” />
<Slider Value=”{Binding Luminosity}” />
<Label Text=”{Binding Luminosity, StringFormat=’Luminosity = {0:F3}’}” />
</StackLayout>
</StackLayout>
</ContentPage>
Using string formatting in the different controls makes our task easy and more attractive. We use string formatting to manipulate different objects or any text we want changes in. String formatting helps us to bind the data with controls and display it properly.
Mainly, we use this for defining a unique format for the binding value or blending the value of any control with another new text.
Author Bio: Vinod Satapara – Technical Director, iFour Technolab Pvt. Ltd.
Technocrat and entrepreneur with years of experience building large scale enterprise web, cloud and mobile applications using latest technologies like ASP.NET, CORE, .NET MVC, Angular and Blockchain. Keen interest in addressing business problems using latest technologies and help organization to achieve goals.
Hire Asp.net developers from iFour Technolab to get your requirements done.
You have heard this far too much, and for good reason. Web design is a… Read More
Florida has a large number of individuals that have an interest in real estate and… Read More
Today, no business is an exception in terms of employing Artificial Intelligence. While healthcare facilities… Read More
Choosing an engagement ring is an amazing event that marks the start of a lifelong… Read More
Artificial Intelligence (AI) and Machine Learning (ML) are reshaping industries by simplifying complex tasks, improving… Read More
Sports betting has been an entertaining pastime for decades, and the birth of the internet… Read More