Wednesday 29 August 2012

Windows 8 Metro apps and the outside world

 In this blog I am sharing one of the new trend in technology world.

'Windows 8 Metro apps and the outside world: connecting with services and integrating the cloud


The blog attachment will covers the overview/discussion about the following :

       •Accessing data behind a service

       •Working with WCF and ASMX services

       •Working with REST services

       •Accessing oData services

       •Syndication with RSS

       •Authenticating with Twitter and Facebook using the WebAuthBroker

       •Using the Live SDK in your Windows 8 apps

       •Using roaming data in your applications

       •Working with sockets             
                –TCP sockets
                WebSockets

      •Setting the correct capabilities for network communication

I have attached the presentation by Gill Cleeren.




Topic summary: Windows 8 Metro style apps will give users a great experience. Apps promise the user to be alive and connected. Being connected means interfacing with services and the cloud. But what options do we have to make the connection to the outside world? 
In this discussion, we could get to know to explore how WinRT apps can connect with services (WCF, REST...) and the cloud to offer a great experience.


Hope this sharing content was useful.

Thanks & Regards
Prabhakaran S Pandian

Friday 24 August 2012

Evolution of C# - Part I

Evolution of C#  - Part I

A quick reference about the evolution of c# , this blog will give an insight in C# 1.0,2.0,3.5,4.0.
The below image will give a overview sentence about the main features about the C# versions.



Evolution of C#

C# 2.0


Delegates


A delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at later time.

Example:


using System; 


delegate string ConvertMethod(string inString); 


public class DelegateExample 


            public static void Main() 
            
                    // Instantiate delegate to reference UppercaseString method 
                    ConvertMethod  convertMeth = UppercaseString; 
                    string name = "Dakota";
                        
                    // Use delegate instance to call UppercaseString method 
                    Console.WriteLine(convertMeth(name)); 
             

             private static string UppercaseString(string inputString) 

             {
                    return inputString.ToUpper(); 
             } 
}


C# 2.0 Anonymous Delegates


Before C# 2.0, the only way to use delegates was to use named methods. In some cases, this results in forced creation of classes only for using with delegates. In some cases, these classes and methods are never even invoked directly.

C# 2.0 offers an elegant solution for these methods described above. Anonymous methods allow declaration of inline methods without having to define a named method. 
This is very useful, especially in cases where the delegated function requires short and simple code. Anonymous methods can be used anywhere where a delegate type is expected.

Anonymous Method declarations consist of the keyword delegate, an optional parameter list and a statement list enclosed in parenthesis.

Event Handler (without using anonymous methods)

public partial class Form1 : Form 
// This method connects the event handler. 
public Form1() 
InitializeComponent(); 
button1.Click += new EventHandler(ButtonClick); 
// This is the event handling method. 
private void ButtonClick(object sender, EventArgs e) 
MessageBox.Show("You clicked the button."); 
}

Event Handler (using anonymous methods)

public partial class Form1 : Form 
public Form1() 
InitializeComponent(); 

button1.Click += delegate(object sender, EventArgs e) 
// The following code is part of an anonymous method. 
MessageBox.Show("You clicked the button, and " + "This is an anonymous                                 method!"); 
}; 
}

One more example:

public partial class Form1 : Form 
{
public Form1() 
InitializeComponent(); 
// Declare the delegate that points to an anonymous method. 
EventHandler clickHandler = delegate(object sender, EventArgs e) 
MessageBox.Show("You clicked the button, and " + "This is an anonymous method!"); 
}; 
// Attach the delegate to two events. 
button1.Click += clickHandler; 
button2.Click += clickHandler; 
}


C# 2.0 Iterators


Iterator is one of the new feature in c# 2.0 which provide a way to create classes that can be used with foreach statement without implementing the IEnumerator & IEnumerable interfaces when compiler detects iterator it will automatically generate the current, MoveNext and dispose method of IEnumerable or IEnumerator interface.

foreach (item OrderItem in catalog) 
// (Process OrderItem here.) 
}



Enumerator e = catalog.GetEnumerator(); 
while (e.MoveNext()) 
OrderItem item = e.Current; 
// (Process OrderItem here.) 
}

one more example:

public class LinkInfo 
    { 
        public string FileName { get; set; }
        public string  LinkContent {  get; set;  } 
        public string  LinkStatus {  get; set;  }
        public string  NewLink {  get; set;  } 
    }

    public static IEnumerable<LinkInfo> GetLinksFromListView(ListView lstLinkList) 
    { 
        foreach (ListViewItem li in lstLinkList.Items) 
        {
            LinkInfo newLink = new LinkInfo(); 
            newLink.FileName = new string(li.Text.ToCharArray());
            newLink.LinkContent =  new string (li.SubItems[1].Text.ToCharArray()); 
            newLink.LinkStatus =  new string (li.SubItems[2].Text.ToCharArray()); 
            newLink.NewLink =  new string (li.SubItems[3].Text.ToCharArray()); 
            yield return newLink; 
        } 
    } 


C# 2.0 Partial Classes


Partial class was introduced in C#2.0 , it means that your class can be divied into multiple segments physically but at the time of compilation it will we grouped as a single entity.


// Stored in file MyClass1.cs 
public partial class MyClass 
public MethodA() 
{...} 

partial  public  MethodB(); 

// Stored in file MyClass2.cs 
public partial class MyClass
public MethodB() {...} 
}


C# 2.0 Generics


Generics is a most powerful feature in .net.Generics allow you to define type-safe classes,collections,remoting,arrays etc..this will improve the performance in your code and leads to a quality code.


public class ObjectList<ItemType> : CollectionBase

private ArrayList list = new ArrayList(); 

public int Add(ItemType value) 

return list.Add(value); 


public void Remove(ItemType value) 

list.Remove(value); 


public ItemType this[int index] 

get { return (ItemType)list[index]; } 
set { list[index] = value; } 

}




// Create the ObjectList instance, and 
// choose a type (in this case, string). 
ObjectList<string> list = new ObjectList<string>(); 

// Add two strings. 
list.Add("blue"); 
list.Add("green");

// The next statement will fail because it has the wrong type. 
// In fact, this line won't ever run, because the compiler
// notices the problem and refuses to build the application. 
list.Add(4);


C# 2.0 Generics constraints


ConstraintDescription
where T: structThe type argument must be a value type
where T: classThe type argument must be a reference type
where T: new( )The type argument must have a public default constructor
where T: <base class name>The type argument must be the base class name or derived from the base class name.
where T: <interface name>The type argument must be the interface or implement the interface. Multiple interfaces may be specified.


Hope this post is useful...
Will be concentrate on the C#3.0/3.5 & 4.0 in Part II.

Thanks & Regards
Prabhakaran S Pandian

Wednesday 22 August 2012

Gender Selection in RadioBox Control with IValueConverter in WPF

Binding Gender in WPF Radiobutton Control


This is one of my interesting post , when answering to one of the Code project question I did this sample WPF application with Data grid and Radio box control to select the gender of the employee and the same I would like to share in the blog.

The question is here to select an employee from the Data grid and display the details in  Text box's and for gender with Radio button.Here the major part is to concentrate on the gender field.The below image shows the output.



With some of the Google leanings came up with a solution to have an enumeration value  for the gender field and using the IValueConverter interface. Use a ValueConverter combined with ConverterParameter to bind the enumeration to the boolean IsChecked of the radio button control.


The below is the code sample's:

XAML Code:



<Window x:Class="SampleWPFApp.GridWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Converters="clr-namespace:SampleWPFApp"
        Title="GridWindow" Height="300" Width="300">
    <Window.Resources>       
        <Converters:EnumBooleanConverter x:Key="EnumGender"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid DataContext="{Binding ElementName=PersonDatagrid,Path=SelectedItem}">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="30"></RowDefinition>                
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            
            <TextBlock Name="Name" Grid.Row="0" Grid.Column="0" Text="Name" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox Name="txtName" Grid.Row="0" Grid.Column="1" Height="20" Text="{Binding Name}"/>

            <TextBlock Name="txtgender" Grid.Row="1" Grid.Column="0" Text="Gender" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <RadioButton GroupName="male" Content="Male" Height="16" Margin="1,5,0,0" Name="MaleRadioButton" VerticalAlignment="Top"  Grid.Column="1" HorizontalAlignment="Left" Width="42" Grid.Row="1" 
                         IsChecked="{Binding Converter={StaticResource EnumGender},Path=Gender,Mode=TwoWay,ConverterParameter=Male}"/>
            <RadioButton GroupName="female" Content="Female" Height="16" HorizontalAlignment="Left" Margin="56,5,0,0" Name="FemaleRadioButton" VerticalAlignment="Top" Grid.Column="1" Grid.Row="1" 
                         IsChecked="{Binding Converter={StaticResource EnumGender},Path=Gender,Mode=TwoWay,ConverterParameter=Female}"/>
        </Grid>

        <DataGrid AutoGenerateColumns="False"  Height="200" HorizontalAlignment="Left" Grid.Row="1" Margin="0,12,0,0" Name="PersonDatagrid" VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Gender" Width="150" Binding="{Binding Gender}"/>               
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

Codebehind code for the XAML page:


namespace SampleWPFApp
{
    /// <summary>
    /// Interaction logic for GridWindow.xaml
    /// </summary>
    public partial class GridWindow : Window
    {
        public GridWindow()
        {
            InitializeComponent();
            PersonDatagrid.ItemsSource = new List<PersonalInfo> { 
                new PersonalInfo { Gender =  Genders.Female , Name = "Mariya"},
                new PersonalInfo { Gender = Genders.Male , Name = "Martin"},
                new PersonalInfo { Gender =  Genders.Female , Name = "Jermila"},
                new PersonalInfo { Gender = Genders.Male , Name = "Chand"}};
        }
    }

    public class PersonalInfo
    {
        public string Name { get; set; }
        public Genders Gender { get; set; }
    }

    public enum Genders
    {
        Male = 1,
        Female
    }
}


Converter code for the control:



public class EnumBooleanConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object paramvalue = Enum.Parse(value.GetType(), ParameterString);
            return paramvalue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            return Enum.Parse(targetType, ParameterString);
        }

        #endregion
    }




Hope this blog post will be useful.

Thanks & Regards
Prabhakaran Soundarapandian