Category Archives: Windows Phone

Windows Phone Database Tutorial

[image2.png]When creating applications for the Windows Phone, there is one basic thing that almost ALL applications need to do, Save Data. If its more than just small bits of data you will probably want to store it in a database. In Windows Phone, this can be done easily, but in different ways.  One of the people on my team Jerry Nixon who is a developer evangelist on my team wrote a great series on this.  The first one is using the Code First approach.

Storing Windows Phone application data is a pretty normal requirement. If you want it to persist across sessions, or even phone reboots, you really have three options:

  1. Save to the Cloud
  2. Save to Isolated Storage
  3. Save to Local Database

Each is correct for certain scenarios. But in this article, I will ONLY walk through the Local Database. This is an enhanced version of SQL CE – meaning your SQL skills are an asset. And, if you don’t have them, they …

Read the whole article  HERE

Related Posts:



WP7 Rest … The Easy Way

Over and over again, I see developers that have this great idea for an application that pulls cool data from a  great services BUT get tied up in the implementation of “Getting It Done”.  So I thought I would show you a quick and easy way to get REST data from a service, transfer the JSON data to a POCO list and Bind it to a listbox.  (In under 3 minutes)

On your Mark

Get Set

Go.

The first think you need to do is find some Data that is interesting to you.  For me, I LOVE coffee so I want to create an application that shows me all the coffee places that are near to me.  I can do this using Bing Search  http://msdn.microsoft.com/en-us/library/dd251014.aspx 

Specifically, I am going to use the PhoneBook Request part of the API that is like a Yellow Pages API.  It will give me all the business that designate themselves as “Coffee” places.

(Yes I know I am not using MVVM)

The first thing I do is Create a method called GetData() and call it from my MainPage() Constructor

  public MainPage()
        {
            InitializeComponent();
            GetData();
        }

        private void GetData()
        {
            string uri = "http://api.bing.net/json.aspx?AppId=YOUR_APP_ID&Version=2.0&Market=en-US&Query=Coffee&Sources=phonebook&latitude=33.8563&longitude=-118.1232";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));
            request.BeginGetResponse(new AsyncCallback(ReadCallback), request);

        }

In GetData() the first thing I do is make a call to the BING  API. 

The first part of the call shows that I want json returned and supplies my App_ID (Removed from post)  ( You can get your own Bing API key here http://msdn.microsoft.com/en-us/library/dd251020.aspx )

http://api.bing.net/json.aspx?AppId=YOUR_APP_ID

Next I set the version, Market, and what I am looking for

&Version=2.0&Market=en-US&Query=Coffee&

And finally, I tell it I am looking at the phonebook part of the API and where I want to search… In this example I am hardcoding the lat/long but you would of course use the Geo Location from the phone, but I wanted to keep this simple.

Sources=phonebook&latitude=33.8563&longitude=-118.1232";

Next, We then need to make the call to the service.  We will use a HttpWebRequest by passing it the URI we just created

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));

And then making in the call and supplying it the CallBack method (Where the call returns to when it is finished)

request.BeginGetResponse(new AsyncCallback(ReadCallback), request);

Now here is the fun part. To work with the JSon data the is returned from this query, we need to create the class structure that represents the data…….. ugggggg what a pain right?   Nope Wrong, All we need to do is use http://json2csharp.com/    Just paste in either the JSon returned OR the URI String, and it will create the classes for you. So when I paste in my URI String above, it gives me the following.

(you can, of course, break up these classes into separate files but for now, I just pasted them at the bottom of the MainPage.xaml.cs file.)

 

public class Query
    {
        public string SearchTerms { get; set; }
    }

    public class Result
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public string Business { get; set; }
        public string PhoneNumber { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string StateOrProvince { get; set; }
        public string CountryOrRegion { get; set; }
        public string PostalCode { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string UniqueId { get; set; }
        public string DisplayUrl { get; set; }
    }

    public class Phonebook
    {
        public int Total { get; set; }
        public int Offset { get; set; }
        public string LocalSerpUrl { get; set; }
        public string Title { get; set; }
        public List<Restuarnt> Results { get; set; }
    }

    public class SearchResponse
    {
        public string Version { get; set; }
        public Query Query { get; set; }
        public Phonebook Phonebook { get; set; }
    }

    public class RootObject
    {
        public SearchResponse SearchResponse { get; set; }
    }

Now that you have the classes that hold the json that is returned, all you need to do is create the ReadCallback Method (Defined above)

private void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
            {
                string resultString = streamReader1.ReadToEnd();
                var ser = new DataContractJsonSerializer(typeof(RootObject));

                var stream = new MemoryStream(Encoding.Unicode.GetBytes(resultString));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
                RootObject myBook = (RootObject)jsonSerializer.ReadObject(stream);

                Deployment.Current.Dispatcher.BeginInvoke(() => coffeeShops.ItemsSource = myBook.SearchResponse.Phonebook.Results); 

            }
        }

You can see that we use a stream reader to read the results and then use a DataContractJsonSerializer to add the data to the RootObject (Root Object is one of the classes that were created for us that encapsulates all the others)

On the last line, I marshall the data to the UI thread using Dispatcher.BeginInvoke and bind it to a ListBox called coffeeShops.

That’s IT….

Now it took me 30 minutes to write this up, but it should take you only 3 minutes to implement.

Enjoy Smile

Related Posts:



The Tale of 3 Platforms

It was the best of times… it was the worst of times….  ok..ok… it is really the best of times to me.  The tech world continues to push all things into a mobile format and I am loving it.

I certainly have my opinions on operating systems, and ease of development, and best IDEs etc… but I thought I would put this to the test with a multi platform development approach.  Starting next week, I will be doing the same application on all three platforms (or possibly 6 if I include IPad, Android Tablet and Win 8 later).   The application is called Uncorked, and it is a way to log and rate your wine collection.  It will include the following features :

1) The use of a Wine API to store my data

2) The ability to scan my bottle to either look up or rate the bottle

3) Room for tasting notes

4) Saving of label pictures

5) And a few data points, price, when to drink by, purchase date, and info about the wine.

V1 will keep it simple so we can write all three in a quick fashion (I hope).

I will start from absolute 0… Brand new machine ( which I will blog about), install of tools, testing, patterns, etc…

I will not be using Phone Gap for this project ( But will be doing another series along side this one using phone gap and a different app)

Feel free to comment, make request, help out, debate, or just read the series.  I hope you get as much out of it as I hope to.

Daniel

Others in this series

Related Posts:



What is 4G

As I talk to phone owners I find it funny when people talk about 3G 4G etc… I ask them if they know what they mean and most of them say no… but they know it means faster Smile  ….   “G” is not a technology but it just is an abbreviation for Generation  4G = Fourth Generation.

But within those generations, there IS the technology that makes them faster.  I found a great video that explains the complexities simply.

“If you cant explain it simply, you don’t understand it well enough” ~ Albert Einstein

Now you know Smile

Related Posts:



Free Windows Phone

image

Are you looking for a Free Windows Phone?  Are you a developer?  Do you live on the West Coast of the US?  Then I have a phone for you. (Read Below)  If you have been looking for a free windows phone to do development on then all you need to do is contact me.  Email me at Daniel.Egan@microsoft.com and I will send you all the details.

I want to be clear on a couple of things.

  1. I am getting these phones to developers that are writing applications for the marketplace
  2. I have a limited supply (First ones to get me the info I need)
  3. You must live on the West Coast of the US

If you do not live on the West Coast of the US, I can try to find your windows phone champ for you.  Just post the request here :  http://thesociablegeek.com/forum/?mingleforumaction=viewforum&f=3.0 

This does not mean that they will have a phone for you, but if you are developing for Windows Phone, you WANT to know your windows phone champ.  If they don’t have phones, they may know someone who does ( I repeat, I am not saying they have phones Smile , but they may know a way.  Either way, they are a great resource for you. )

So all of you West Coasters, get your Free Windows Phone while they last.  Email me at Daniel.Egan@microsoft.com for the details.

Related Posts:



Windows Phone Feedback

WPDev FeedbackYou now have multiple ways to give your developer feedback to Microsoft on how change Windows Phone 7.  You can still use the uservoice forums to either vote for an idea OR add your own.

http://wpdev.uservoice.com/forums/110705-app-platform

But now, you can also use an application on your phone. Check out the app and the website, give your feedback and let us know how we can make Windows Phone 7 even better.

Here is a link to the application in the marketplace.

http://www.windowsphone.com/en-US/apps/b5466109-2b8d-46f4-9461-c959e433ae4a

or scan this tag to go to the app on your Windows Phone 7.

WPDev Feedback

Related Posts:

  • No Related Posts


See Through Windows Phone

Bouncing around checking out blogs, I found this great video on Youtube about the Microsoft prototype lab.  The coolest part was looking at the Clear (See through) Windows Phone… OH I Wish Smile

Related Posts:

  • No Related Posts


Skin your Windows Phone Emulator

wp7EmulatorSkinSwitcher

Geert van der Cruijsen has created a tool that makes it easy for you to change the skin on your windows phone emulator. Its pretty cool. It allows you make your emulator look like one of the new Nokia Phones.

Keep in mind that it is beta.. use at your own risk Winking smile 

Check it out here.

http://vdcruijsen.net/2011/12/windows-phone-7-emulator-skin-switcher-1-0-beta/

Related Posts:

  • No Related Posts


PhoneGap for Windows Phone

 

If you are writing your phone applications using HTML5, you will want to check this out. PhoneGap has released an update to work with Windows Phone.  What PhoneGap does is allow you to write your phone application in HTML5 and still access most of the Native API’s and wrap it in a Native application wrapper so that you can put it in the marketplace.  This means you can write your application cross platform.

You can download it from here : http://phonegap.com/  or just watch the video below for more information.

Related Posts:



The Web Role was started in a wrong port

imageOne of the great things about Windows Phone is all of the samples that are out there.  And topping them all is the Windows Azure Toolkit for Windows Phone 7 .  The toolkit gives you everything you need to develop a Windows Phone application using Windows Azure. I will do a full post walkthrough of using the toolkit, but I thought I would post about one thing that seems to be tripping people up when they are trying to get things up and running.

If you start the project and get the error Sorry, an error occurred while processing your request, the web role was started in a wrong port, You might scratch your head.  If you peek at the url you will notice that it is running on port 444 instead of 443

image

The reason it is doing this is because if it does not find port 443 open, it will increment by one until it finds an open port. If you look in your compute emulator you will also see the wrong port. Which is the reason it says The web role was started in a wrong port.  Because it was :)

image

So now, to fix this you want to see what is running on port 443.  To do this open a cmd window and type in the following netstat –aon

This will bring up a list of what is running on each port.

image

You want to look for anything that has a :443 at the end of it and look at the last column.  This is the process ID for the process that is using that port.

Next, open up Task Manager and look for the process ids that you found. ( You may need to go to View –> Select Columns first to add the PID column)

 

image

My offending applications were:

  • SQL Reporting Services (Which I shut down in Services)
  • TweetDeck
  • Skype

That fixed it for me… hope that helps

Daniel

Related Posts: