Hi this is a quick and simple WP7 application, which demonstrates some of the advanced features within very few steps.
If you are already familiar wit h WCF and Silverlight then WP7 development is very easy for you.
This application explains the following features.
-
Accessing the WCF service from the WP7 application.
-
Handling Cross reference threads in Wp7 and create responsive UI.
Application
The application asks you simple question, and if you answer correct then you will be taken to meet Jack Sparrow. If you do not answer correctly, then the application asks another question and this process repeats.
WCF service is responsible for generating the questions and evaluating the answer. Thread is used to load the web browser. Browser object is in the Collapsed Visibility mode, and the thread used to navigate the page, and bind the delegate of Navigated event.
Since the web browser is created in the main thread and accessed in another thread, this creates the cross thread reference problem. It is solved using the traditional way of the BeginInvoke() call. But the BeginInvoke() call is made from the Dispatcher object of the current thread.
Code Implementation
In my WCF service I have 2 operational contracts. One is to generate the question and the other one is for evaluate the answer. (Read the comments within the code blocks)
Code for my IService1.cs
Code implementation of those methods are mentioned here, You can download the complete solution here
As like Silverlight we have to call the WCF service through asynchronous method calls and binding a delegate to that. But we do not have to worry much since VS generates the Async methods for us.
Build the service, and the reference of the Service to the WP7 application.
The below image shows my WP7 application interface in the design mode.
In the MainPage.xaml.cs file we have code logic of the application.
Here is the code for calling a service in WP7. It is 100% similar to Silverlight applications. (Read the comments ..)
Once the Method call is returned from the service, the following method will be fired. (Note that GetQuestion() is the method we created. GetQuestionAsync() is generated by VS. We have to simply call that. (If you are familiar with Silverlight then this is not a big deal for you)).
The following method handles the cross thread reference problem, and provides a responsive UI. (Note that I didn’t code any explicit Threads. In .NET you can do this with simple delegates, .NET handles the thread for you).
Notice that in the BeginInvoke() I’m again making a call to my main thread for the Navigated event.
After the application completes it happily displays Jack Sparrow