Today in London we ran our first Windows Phone 7 app to Windows 8 porting event. The idea was for people to come along and take their existing Windows Phone 7 applications and turn them into Windows 8 applications. I thought I’d keep a record of the things that we have helped resolve throughout the day in the hope that we would end up with a list of common issues.
One of the big Differences between WP7 and Windows 8 is the new Async feature of Windows 8. In windows 8 If the API is likely to take longer than around 50ms to execute, it is asynchronous. In some instances Synchronous methods have been replaced with asynchronous ones and this trips up a number of WP7 developers when they try and port their code.
Two really good blog posts on the subject can be found here:
Does the device have an internet connection?
A couple of Windows Phone 7 apps used a very simple mechanism to determine if the device had an internet connection on Windows Phone 7. The common code was:
Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet
The equivalent namespace in windows 8 is Windows.Networking.Connectivity. A simple test to see if you have a network of any kind is to use the code below. If InternetConnectionProfile is null there is no connection.
ConnectionProfile InternetConnectionProfile = NetworkIntormation.GetInternetConnectionProfile();
If it it not null you can ascertain all sorts of information about the connection. For example if the user is roaming or if the user is approaching their data limit.
var cost = InternetConnectionProfile.GetConnectionCost();
var isApproachingDataLimit = cost.ApproachingDataLimit;
var isRoaming = cost.Roaming;
Performing a post to receive an image
This was kind of a strange issue. One of the apps needed to post to a URL in order to receive an image from a server. Otherwise the image would return with a “You are not allowed to Embed this image warning” after trying a number of things we finally got the following code to work.
HttpClient client = new HttpClient();
HttpContent test2 = null;
var url = "http://www.theimage";
HttpResponseMessage response = await client.PostAsync(url, test2);
byte bytes = await response.Content.ReadAsByteArrayAsync();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
BitmapImage img = new BitmapImage();