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.
Building a robust online presence is no longer optional for entrepreneurs; it’s a necessity. Websites… Read More
Advanced application security testing tools are key to the rapid pace of digital transformation. Applications… Read More
Endpoint management is a superhero today. It caters to various requirements of an organization. These… Read More
Today, it is impossible to conduct business by ignoring the online presence; therefore, it is… Read More
Simply put, supply chains are the cornerstone of modern businesses. They effectively connect organizations not… Read More
AI is transforming customer service by making it quicker and more intelligent. Chatbots and AI… Read More