Friday, August 14, 2009

Implementing INotifyPropertyChanged

If an object implementing INotifyPropertyChanged Interface it’ll raise a property changed event when its property changes.Lets create a sample application to know how we can implement INotifyPropertyChanged Interface. I am creating a Silverlight application which demonstrates the data binding with both an object implementing INotifyPropertyChanged and also a normal DependencyProperty.
  • Open Visual Studio and select a new silverlight application.
  • Create a class named Customer in the silverlight project and implement INotifyPropertyChanged.
public class Customer : INotifyPropertyChanged
{
  • Define INotifyPropertyChanged Members,
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(PropertyChangedEventArgs e)
{
   if (PropertyChanged != null)
   {
     PropertyChanged(this, e);
   }
}
  • In property setter invoke OnPropertyChanged by passing property name like,
private string _Name;

public string Name
{
  get
  {
     return _Name;
  }
  set
  {
    _Name = value;
    OnPropertyChanged(new PropertyChangedEventArgs("Name"));
  }
}
  • In MainPage.xaml.cs add an ObservableCollection of customer object as Dependency
    property inorder to make sure that UI is updating while we assigning that customer list
    to another list or object.If we are making it as a normal property UI will update only if
    we add new object to customerlist or any change occurs to the underlying properties.
public ObservableCollection<Customer> CustomerList
{
  get { return (ObservableCollection<Customer>)
GetValue(CustomerListProperty); }
  set { SetValue(CustomerListProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.
This enables animation, styling, binding, etc...
public static readonly DependencyProperty CustomerListProperty =
DependencyProperty.Register("CustomerList",
typeof(ObservableCollection<Customer>), typeof(MainPage),
    new PropertyMetadata(new ObservableCollection<Customer>()));
  • I also added a DependencyProperty FirstName in MainPage.xaml.cs just to show the binding of a simple DependencyProperty.
public string FirstName
{
  get { return (string)GetValue(FirstNameProperty); }
  set { SetValue(FirstNameProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.
This enables animation, styling, binding, etc...
public static readonly DependencyProperty FirstNameProperty =
  DependencyProperty.Register("FirstName", typeof(string), typeof(MainPage),
  new PropertyMetadata(string.Empty)); 
  • In MainPage.XAML add a datagrid and textbox and bind it to the ObservableCollection and DependencyProperty respectively.
<data:DataGrid AutoGenerateColumns="True"
 Width="400"
 Height="300"
 ItemsSource="{Binding ElementName=TestUC,
                                Path=CustomerList}"/>
<TextBox x:Name="NameTextBox"
Text="{Binding ElementName=TestUC, Path=FirstName, Mode=TwoWay}"
Width="100"
Height="25"
Margin="0,10,0,10" />
  • For Understanding PropertyChanged event, I added a button and just updating the customer object in the click event so that you can see the changes in the datagrid.When you change the property of Customer object from click event you can see that the UI is updating accordingly.
  • Download Sample Application.

Thursday, July 30, 2009

Silverlight With WCF Service

How to use a WCF Service from a silverlight client?Let us discuss how we can achieve that.Sample application is also attached in the bottom of this article.
    • Open Visual Studio and Create a new Silverlight application by selecting any of the available project templates in visual studio.Here i am selecting Silverlight Navigation Application,
image
image
This will create a silverlight project and a Web project in the solution.
    • Right Click the solution and Add a New WCF Service Application,
image
    • Create service methods in the WCF Service Application.
    • Add Service Reference to the Silverlight application by right clicking it and select Add Service Reference.
image
    • Add crossdomain.xml file to the WCF Service Project for enabling silverlight application to access the WCF Service.If WCF service is hosted in a web application then add policy file to the web application.
    • Try to access the service methods in silverlight application using the service reference added.
    • Download Silverlight & WCF Service Sample Application.If you get any run time errors for the sample application then delete and add the service reference again in the silverlight project.


Another sample application - A Silverlight RSS Reader

Thursday, July 23, 2009

Microsoft Tag Reader

Today i installed Microsoft Tag Reader in my Mobile - N73 Symbian S60 3rd Edition.Application will decode a tag printed some where by using the mobile camera and also open the related web content.
Download Tag Reader for your mobile and try it out.Its cool.

Silverlight 3 Features

Resources for Silverlight 3 ,

Monday, June 29, 2009

Immediate Loading Relational Data in LINQ to SQL

In LINQ to SQL the relational data is loading only when we refer that data, other terms its lazy loading of data.But we can load relational data ,suppose we have an Employee Table and also having an Employee details table related to Employee Table, we can load that relational data using DataLoadOptions while querying the context.
DataLoadOptions options = new DataLoadOptions();
options.AssociateWith<Employee>(p => p.EmployeeDetails.
Where(q => q.IsActive == false));
options.LoadWith<Employee>(p => p.EmployeeDetails);
Context.LoadOptions = options;
var emp = from p in DataContext.Employee
where p.EmployeeID == employeeID &&
p.IsDeleted == false
select p

Thursday, May 28, 2009

Microsoft Surface Videos

Watch some cool Microsoft Surface Videos here.
Hope you’ll enjoy.