Wednesday, September 26, 2012

Windows Phone and Silverlight FAQ

Recently I was encouraged by the people at work to venture into building Windows Phone (WP) native application.

The enticement was in the form of a Windows Phone; the Nokia Lumia 710, to be exact.

So the following were some caveats that I found that I thought would be interesting to post here just in case someone else also happen to encounter the same issues.

Issue 1
Displaying images dynamically; threading issue.

First of all, I understood the way to retrieve images dynamically from say somewhere in the world wide web. That was not my problem and should you have troubles with that, this is not the place to look.

WP works on threads. My XAML had a image tag with a name of "imgOne" for simplicity purposes and I need to change the source based on some user input.

In the code behind, the XAML.cs file, you will have access to the image tag should you type "imgOne." and Visual Studio intellisense should bring up the available properties for "imgOne".

However, if you attempt to set the source there; imgOne.Source = someBitmapObject
You will encounter the UnhandledAccessException similar to something like this


To fix this problem, you would have to use the Dispatcher.BeginInvoke function. Something similar to this

Issue 2

My application is a very small one, so the route I took with it was to not implement MVVM, although it would have been easier to have done so.

So, if you would like to go down this path, here goes.

Each item in the XAML has a DataContext and by default, each of these data context works in a "One Way" mode. So once you set it, the DataContext won't change unless you reset it but here comes the wacky part.

You will have to set the DataContext to null before resetting it to another object.