MainPage.Xaml

<phoneNavigation:PhoneApplicationPage

x:Class=“ConsumingODATA.MainPage”

xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:phoneNavigation=“clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation”

xmlns:d=http://schemas.microsoft.com/expression/blend/2008&#8221;

xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006&#8221;

mc:Ignorable=“d” d:DesignWidth=“480” d:DesignHeight=“800”

FontFamily=“{StaticResource PhoneFontFamilyNormal}”

FontSize=“{StaticResource PhoneFontSizeNormal}”

Foreground=“{StaticResource PhoneForegroundBrush}”>

<Grid x:Name=“LayoutRoot” Background=“{StaticResource PhoneBackgroundBrush}”>

<Grid.RowDefinitions>

<RowDefinition Height=“Auto”/>

<RowDefinition Height=“*”/>

</Grid.RowDefinitions>

<!–TitleGrid is the name of the application and page title–>

<Grid x:Name=“TitleGrid” Grid.Row=“0”>

<TextBlock Text=“Windows 7 Phone” x:Name=“textBlockPageTitle” Style=“{StaticResource PhoneTextPageTitle1Style}”/>

<TextBlock Text=“ODATA” x:Name=“textBlockListTitle” Style=“{StaticResource PhoneTextPageTitle2Style}”/>

</Grid>

<!–ContentGrid is empty. Place new content here–>

<Grid x:Name=“ContentGrid” Grid.Row=“1”>

<Grid.RowDefinitions>

<RowDefinition Height=“*” />

<RowDefinition Height=“4*” />

</Grid.RowDefinitions>

<Button x:Name=“myButton” Height=“100” Content=“Get Data” Grid.Row=“0” Background=“Azure”

Foreground=“Black”/>

<ListBox Grid.Row=“1” x:Name=“MyList”

ItemsSource=“{Binding}” Height=“458”

HorizontalAlignment=“Left” Margin=“20,39,0,0” VerticalAlignment=“Top” Width=“435” >

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel Orientation=“Horizontal” >

<TextBlock Text=“{Binding RollNo}” Foreground=“Red” Margin=“20” />

<TextBlock Text=“{Binding Name}” Margin=“20” />

<TextBlock Text=“{Binding Subject}” Margin=“20” />

</StackPanel>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

</Grid>

</Grid>

</phoneNavigation:PhoneApplicationPage>

MainPage.Xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

using System.Data.Services.Client ;

using StudentDBModel;

namespace ConsumingODATA

{

public partial class MainPage : PhoneApplicationPage

{

StudentDBEntities studentEntities = null ;

List<Student> lstStd  = null ;

List<StudentEntity> lstResultStd = null ;

public MainPage()

{

InitializeComponent();

SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

studentEntities = new StudentDBEntities(new Uri ("http://b263ltrv:3803/WcfDataService1.svc/"));

lstResultStd = new List<StudentEntity>();

lstStd = new List<Student>();

myButton.Click += new RoutedEventHandler(myButton_Click);

}

void myButton_Click(object sender, RoutedEventArgs e)

{

var query = studentEntities.CreateQuery<Student>("Students");

query.BeginExecute(ar=>

{

GetData(ar);

},query);

}

public void GetData(IAsyncResult result)

{

DataServiceQuery<Student> query = result.AsyncState as DataServiceQuery<Student>;

lstStd = query.EndExecute(result).ToList();

Dispatcher.BeginInvoke(()=>

{

foreach(var r in lstStd)

{

lstResultStd.Add(new StudentEntity{RollNo=r.RollNumber ,

Name = r.Name ,

Subject = r.Subject });

}

this.MyList.DataContext = lstResultStd ;

});

}

}

}

StudentEntity.cs

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace ConsumingODATA

{

public class StudentEntity

{

public string RollNo { get; set; }

public string Name { get; set; }

public string Subject { get; set; }

}

}

Student.cs

//------------------------------------------------------------------------------

// <auto-generated>

//     This code was generated by a tool.

//     Runtime Version:4.0.30319.1

//

//     Changes to this file may cause incorrect behavior and will be lost if

//     the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------

// Original file name: Student.cs

// Generation date: 7/5/2010 6:06:44 PM

namespace StudentDBModel

{

/// <summary>

/// There are no comments for StudentDBEntities in the schema.

/// </summary>

public partial class StudentDBEntities : global::System.Data.Services.Client.DataServiceContext

{

/// <summary>

/// Initialize a new StudentDBEntities object.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public StudentDBEntities(global::System.Uri serviceRoot) :

base(serviceRoot)

{

this.OnContextCreated();

}

partial void OnContextCreated();

/// <summary>

/// There are no comments for Students in the schema.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public global::System.Data.Services.Client.DataServiceQuery<Student> Students

{

get

{

if ((this._Students == null))

{

this._Students = base.CreateQuery<Student>("Students");

}

return this._Students;

}

}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

private global::System.Data.Services.Client.DataServiceQuery<Student> _Students;

/// <summary>

/// There are no comments for Students in the schema.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public void AddToStudents(Student student)

{

base.AddObject("Students", student);

}

}

/// <summary>

/// There are no comments for StudentDBModel.Student in the schema.

/// </summary>

/// <KeyProperties>

/// RollNumber

/// </KeyProperties>

[global::System.Data.Services.Common.DataServiceKeyAttribute("RollNumber")]

public partial class Student

{

/// <summary>

/// Create a new Student object.

/// </summary>

/// <param name="rollNumber">Initial value of RollNumber.</param>

/// <param name="name">Initial value of Name.</param>

/// <param name="subject">Initial value of Subject.</param>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public static Student CreateStudent(string rollNumber, string name, string subject)

{

Student student = new Student();

student.RollNumber = rollNumber;

student.Name = name;

student.Subject = subject;

return student;

}

/// <summary>

/// There are no comments for Property RollNumber in the schema.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public string RollNumber

{

get

{

return this._RollNumber;

}

set

{

this.OnRollNumberChanging(value);

this._RollNumber = value;

this.OnRollNumberChanged();

}

}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

private string _RollNumber;

partial void OnRollNumberChanging(string value);

partial void OnRollNumberChanged();

/// <summary>

/// There are no comments for Property Name in the schema.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public string Name

{

get

{

return this._Name;

}

set

{

this.OnNameChanging(value);

this._Name = value;

this.OnNameChanged();

}

}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

private string _Name;

partial void OnNameChanging(string value);

partial void OnNameChanged();

/// <summary>

/// There are no comments for Property Subject in the schema.

/// </summary>

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

public string Subject

{

get

{

return this._Subject;

}

set

{

this.OnSubjectChanging(value);

this._Subject = value;

this.OnSubjectChanged();

}

}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]

private string _Subject;

partial void OnSubjectChanging(string value);

partial void OnSubjectChanged();

}

}

Advertisements

2 thoughts on “Video on consuming WCF Data Service (OData) in Windows 7 Phone Application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s