Search This Blog

Thursday 28 November 2019

Xamarin Form Date Format on XAML Page

<Label Text="{Binding Date,StringFormat='{0:MM/dd/yy}'}" />  
Output: 05/25/2019


Date Formats:
DateTime.Now.ToString("MM/dd/yyyy")05/29/2015
DateTime.Now.ToString("dddd, dd MMMM yyyy")Friday, 29 May 2015
DateTime.Now.ToString("dddd, dd MMMM yyyy")Friday, 29 May 2015 05:50
DateTime.Now.ToString("dddd, dd MMMM yyyy")Friday, 29 May 2015 05:50 AM
DateTime.Now.ToString("dddd, dd MMMM yyyy")Friday, 29 May 2015 5:50
DateTime.Now.ToString("dddd, dd MMMM yyyy")Friday, 29 May 2015 5:50 AM
DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")Friday, 29 May 2015 05:50:06
DateTime.Now.ToString("MM/dd/yyyy HH:mm")05/29/2015 05:50
DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")05/29/2015 05:50 AM
DateTime.Now.ToString("MM/dd/yyyy H:mm")05/29/2015 5:50
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")05/29/2015 5:50 AM
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")05/29/2015 05:50:06
DateTime.Now.ToString("MMMM dd")May 29
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")2015-05-16T05:50:06.7199222-04:00
DateTime.Now.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")Fri, 16 May 2015 05:50:06 GMT
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")2015-05-16T05:50:06
DateTime.Now.ToString("HH:mm")05:50
DateTime.Now.ToString("hh:mm tt")05:50 AM
DateTime.Now.ToString("H:mm")5:50
DateTime.Now.ToString("h:mm tt")5:50 AM
DateTime.Now.ToString("HH:mm:ss")05:50:06
DateTime.Now.ToString("yyyy MMMM")2015 May

For more details about date format, you can visit this link 

Tuesday 26 November 2019

Handling Single Object and Multiple Object JSON Converter

Deserializing Single Object and Multiple Object using JSON Converter.


JSON Sample
this is Single Json Object 
{
 "Name":"Test",
 "Email":"Abc",
 "ID":1
}     
Multi Json Object
[
   {
     "Name": "Test",
     "Email": "Abc",
     "ID": 1
   },
   {
     "Name": "Test1",
     "Email": "Abc1",
     "ID": 2
   }
]


List<Employee> list = JsonConvert.DeserializeObject<List<Employee>>("string json Content");

Here if your JSON string contain single object that time it cause error like "'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List'."

so to avoid this use the JSON Converter.

Example: 
JsonSerializerSettings setting = new JsonSerializerSettings();
setting.Converters.Add(new SingleOrArrayConverter<Employee>());
 
List<Employee> list = JsonConvert.DeserializeObject<List<Employee>>("string json Content",setting);



Custom Class For Converting Object Result into JSON

Public class SingleOrArrayConverter<T> : JsonConverter
{
     public override bool CanConvert(Type objectType)
     {
         return (objectType == typeof(List<T>));
     }
     public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
     {
         JToken token = JToken.Load(reader);
         if (token.Type == JTokenType.Array)
         {
             return token.ToObject<List<T>>();
         }
         return new List<T> { token.ToObject<T>() };
 
     }
     public override bool CanWrite
     {
         get { return false; }
     }
     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
     {
         throw new NotImplementedException();
     }
 
}


Monday 25 November 2019

Convert XML To Json with NULL Value Handling, Remove Empty Element from XML Node

Convert XML To Json with NULL Value Handling

XDocument doc = XDocument.Parse("Specify the XML Content in String Format");
 
doc.Descendants().Where(f => f.IsEmpty || string.IsNullOrWhiteSpace(f.Value)).Remove();
 
string jsonText = JsonConvert.SerializeXNode(doc); // this will convert XNode to Json Text
Namespaces used
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Linq; 

Sunday 24 November 2019

Xamarin Forms Trigger (Data Trigger, Property Trigger)

Property Trigger 

Property triggers occur when a property on control is set to a particular value.



Page1.xaml
<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestAppDemo.Page1">
    <ContentPage.Content>
        <StackLayout Margin="10">
            <Label Text="Property Trigger" FontSize="Large" />
            <Entry Placeholder="Enter Text">
                <Entry.Triggers>
                    <Trigger TargetType="Entry" Property="IsFocused" Value="True">
                        <Setter Property="BackgroundColor" Value="LightBlue" />
                    </Trigger>
                </Entry.Triggers>
            </Entry>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>





Data Trigger 

Data triggers occur when a property of other control is set. And also you can specify the binding from ViewModel.






Page1.xaml
<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestAppDemo.Page1">
    <ContentPage.Content>
        <StackLayout Margin="10">
            <Label Text="Data Trigger" FontSize="Large" />
            <StackLayout Orientation="Horizontal">
                <CheckBox x:Name="chkBox"   />
                <Label Text="IsCheckbox checked" VerticalTextAlignment="Center"  >
                    <Label.Triggers>
                        <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference chkBox},Path=IsChecked }" Value="True" >
                            <Setter Property="Text" Value="Yes!" />
                        </DataTrigger>
                        <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference chkBox},Path=IsChecked }" Value="False" >
                            <Setter Property="Text" Value="No!" />
                        </DataTrigger>
                    </Label.Triggers>
                </Label>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
 

Top 60 Xamarin Blog 


Friday 8 November 2019

Deep Cloning of Object in Xamarin - C#

Using newtonsoft json you can achieve deep cloning easily.

var serialized = JsonConvert.SerializeObject(AnyObject);
var deserializedObject JsonConvert.DeserializeObject<TypeOfObject>(serialized);


Sample

List<Employee> emp= new List<Employee>();
List<Employee> emp1 = new List<Employee>();
emp1=emp;

 Now if you update the emp1 data it reflect the emp object too.
 for avoid this you can use object cloning.

 var serialized = JsonConvert.SerializeObject(emp);
 var deserializedObject = JsonConvert.DeserializeObject<List<Employee>>(serialized);
Top 60 Xamarin Blog 

Popular Posts