Currently Being Moderated
Below is an example of a simple application for windows mobile 8 consuming OData.

 

I divided the task into 2 steps
  1. Creating a simple c# split application and understanding how the sample data is getting displayed on screen.
  2. Wiring the application to the reference service to show data fetched from web.

 

Simple Split Sample App

 

Under File -> New -> Project selectInstalled -> Templates -> Visual C# -> Windows Store And select Split App(XAML) from the central pane.
screen1.png
Once you click "OK" you have a sample application that will run and show you data! To launch the application hit F5, this launches the application in Debug mode.
On launch of the application you can see the first screen as below
screen2.png
On clicking "Group Title 1" the screen with the list of the item and the details about each item comes up.
screen3.png
Now our task is to find out where the sample data on both these screens came from.  For this look into Solution Explorer -> Solution 'WinOdata' -> DataModel -> SampleDataSource.cs
Search for the method SampleDataSource(). This is where all the data is getting added to both the screens. A new SampleDataGroup class is created, to this new SampleDataItem classes are added, and finally the SampleDataGroup is added to AllGroups which is a collection of SampleDataGroup declared just at the beginning of the SampleDataSource class.
I suggest you play a little with this code and keep running your application with each change to get an idea of how each of these changes affect the display of data on the screens.
With this we have finished our first step of creating a simple application and knowing what code we need to change to show our data instead of the sample data.

 

 

Creating Reference service and displaying OData.

 

Now we will create a reference service. For creating a reference service right click on "WinOData" under "Soluton 'WinOData'" and select "Add Service Reference.."

screen4.png
Fill in as below and click ok.
Namespace: odataService
You can see odataService under Service Reference in Solution Explorer.
Now we will start editing SampleDataSource.cs and wire it to show our data

Add the following line at the top of the class among the other "using" statements.

 

 using WinOData.odataService;

 

We will code a new method CategoryProductList(), we will need this new method as we will be firing async queries and for that the method needs to be async and the SampleDataSource() method cannot be made into async.

 

 async void CategoryProductList()
        {
            DemoService service = new DemoService(new Uri("http://services.odata.org/(S(1mwfq3psy4zegts3wuo5yt4i))/OData/OData.svc"));
           // The below code is required if the service expects authentication
             //  service.Credentials = new NetworkCredential("username", "password", "domain");
            var query = from p in service.Categories
                        select p;
            DataServiceQuery<Category> dqs = (DataServiceQuery<Category>)(query);
            TaskFactory<IEnumerable<Category>> tf = new TaskFactory<IEnumerable<Category>>();
            IEnumerable<Category> b = await tf.FromAsync(dqs.BeginExecute(null, null),
                                      iar => dqs.EndExecute(iar));
            foreach (Category cat in b)
            {
                var group1 = new SampleDataGroup("cat"+cat.ID,
                  cat.Name,
                  "",
                  "Assets/DarkGray.png",
                  "");
                 var oquery = from op in service.Products
                             where op.Category.ID == cat.ID
                            select op;
                 DataServiceQuery<Product> odqs = (DataServiceQuery<Product>)(oquery);
                 TaskFactory<IEnumerable<Product>> otf = new TaskFactory<IEnumerable<Product>>();
                 IEnumerable<Product> ob = await otf.FromAsync(odqs.BeginExecute(null, null),
                                          iar => odqs.EndExecute(iar));
                 foreach (Product products in ob)
                {
                    string description = String.Format("Description:{0}\nPrice:{1}\nRating:{2}\n", products.Description, products.Price, products.Rating);
               
                    group1.Items.Add(new SampleDataItem("product"+products.ID,
                            products.Name,
                            "",
                            "Assets/LightGray.png",
                            "",
                            description,
                            group1));
                }
               this.AllGroups.Add(group1);
            }
            
        }
And Finally the SampleDataSource() method will look like below.
 public SampleDataSource()
        {
            CategoryProductList();
        }
That's it! We are done with our application! Hit F5 and see the Application running. You should see something like below.
screen5.1.png
On clicking Beverages we get the below screen.
screen6.png
  Thus we completed a simple application for windows mobile 8 consuming OData! Hope this helps.

Comments

Actions

Filter Blog

By author:
By date:
By tag: