Pawel Sawicz.NET technical blog, sometimes other stuff

Windows Phone

Few hours ago my second application for Windows Phone just released, this time I made application for Build Stuff conference. Just check this out and please let me know about any issue or things to improve, via mail pawel@sawicz.eu or in comment below.

Of course it’s first release, and application will be updated if any new speaker or session come out.

<Grid> has got various members, for declaring rows and columns we have to use
<Grid.RowDefinitions> and
<Grid.ColumnDefinitions> by these members we can declare width of column, and height of row.

Example implementation :

Example of Rows and Columns

1

2

3

4

5

6

7

8

9

10

11

12

13

<Gridx:Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0">

<Grid.RowDefinitions>

<RowDefinition Height="50"/>

<RowDefinition Height="*"/>

<RowDefinition Height="200"/>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="100"/>

<ColumnDefinition Width="*"/>

<ColumnDefinition Width="50"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

</Grid>

As you can see I made three rows, and four columns. Now if you want place something on specific column or row, it’s similar to game “Battleship“. Let’s place text box, with help comes Grid.Row and Grid.Column.

<TextBox Grid.Row="0"Grid.Column="1"/>

Notice: good practice – you should implement whole sizes at this step (by Row-Column Definitions), because in my opinion using fixed width and height at items property is mistake (of course sometimes you have to use item property), it’s like clothes, you want them to well fit on your body, you do not resize your body to clothes.

Notice: When you are designing your UI by the <Grid> make sure that you are include margin inside these definitions, I think it’s good practices to define spacing by this mechanism.

Nesting

You can define <Grid> inside of <ListBox> (i.e), and you can form <ListBox> view as you wish, it’s very helpful to create custom view of listbox.

I would like to show you, good library to working with REST architecture. Nowadays REST is getting more popular because it’s very simple API, even ASP.NET MVC support this as default API service.

RESTSharp, is powerful library for any kind of .NET technology. Especially for Windows Phone where you have to use REST or SOAP to communicate with your external data. I came across RESTSharp when I was working on Project for Imagine Cup 2013. It saves me, a lot of time. RestSharp support asynchronously actions as well as windows phone because it is the main presumption of programming on that platform. I would like to show you few code block of using RESTSharp. At the begging basics and then in the next blog post some advance methods of using library with async.

C#

1

2

3

varclient=newRestClient("192.168.0.1");

varrequest=newRestRequest("api/item/",Method.GET);

varqueryResult=client.Execute<List<Items>>(request).Data;

First, we have to make a connection with server where API is located. Then we add physical path to specific API controller, and choose method of connection (GET, POST, DELETE, PUT etc.). Next step is create a query to our REST API services, our result will be list of Item, it’s highly recommended to use that type deserialization, now we can easily get access just adding .Data at the end of statement.

Now let’s look at Method.POST.

C#

1

2

3

4

5

6

7

8

9

varclient=newRestClient("http://192.168.0.1");

varrequest=newRestRequest("api/item/",Method.POST);

request.RequestFormat=DataFormat.Json;

request.AddBody(newItem

{

ItemName=someName,

Price=19.99

});

client.Execute(request);

client and request stay same like in first example, but there is one change in method of connection, please set Method.POST. Next step is add body to our request create model that is expected in API controller, and finally last line, execute our request.

If we want run delete method, just use Method.DELETE

C#

1

2

3

4

5

varitem=newItem(){//body};

varclient=newRestClient("http://192.168.0.1");

varrequest=newRestRequest("api/item/{id}",Method.DELETE);

request.AddParameter("id",idItem);

client.Execute(request)

Declaring client stay same like in other cases, request has small change, first you have to add path with parameter and define method of connection Method.GET. Moreover, we have to add to request one more property, define parameter {id} and what values its get. Finally execute request.