I think this is a common and typical question from novices but I still don’t fully understand the given solutions.
I am writing a Windows Phone 7 application and I want to perform several HttpWebRequest where I submit POST data and set some headers. The response should be deserialized and then processed by the logic and/or UI layer.
Yes, Web communication is done in a asynchronous way for Silverlight apps. I got that. It works fine.
BUT: I want to decouple the communication handling from the UI resp. xaml.cs code.
I need to do the communication with HttpWebRequest because I need to modify headers etc.
What I want to do is the following:
- A button is clicked. Login/Username is provided to let’s say my communication layer. For example:
LoginRequest(string username, string pwd) - The comm. layer will execute the request and obtain the answer asynchronously, i.e. calling the callback
LoginResponseCallback(IAsyncResult res) - Okay, now I can handle the response, deserialize my retrieved JSON string into an object
- The object should be handed back to the UI to update the data. I know that I should use the
Dispatcher.BeginInvokemethod because the callback is not running on the UI thread.
But I still don’t know how I can pass the object from my communication layer to the UI.
I think the solution is very straightforward, like passing a delegate from the btnLogin_onClick(object sender, RoutedEventArgs e) to the LoginRequestmethod. Or maybe I need to work with lambdas?
I have a SendBasicRequest method in my communication layer, which sets the appropriate headers (e.g. ClientOS, LoginKey, VersionBuild etc.)
I want to call this method from several pages, so I want it to be decoupled in the communication layer.
Thanks in advance
MVVM Light Toolkit provides a solid framework to use MVVM easily for WP7 apps.
But just be aware, it requires some prereading to be able to fully understand the principles of the MVVM pattern and the frameworks based on MVVM.
Some useful information which may guide you through the complexity. It helped me to decouple a lot of logic from the XAML and the code-behind file with Commands. To use ViewModels to handle the logic and also to run tasks easily async with the Async CTP (fully available in VS2012).
WPF Apps With The Model-View-ViewModel Design Pattern helps you understand MVVM & DataBinding
Getting Started with the MVVM Pattern in Silverlight Applications uses IoC to inject an intermediary service agent, who fetches the data async
Windows Phone Mango: Getting Started with MVVM in 10 Minutes nice and easy to understand how to use MVVM and Commands
Commands in MVVM (Apex framework) features Asynchronous Commands etc.
Async / Await
Helps you a lot to do asynchronous operations in the background and getting notified when tasks are done. You use async/await keywords (new in VS2012 or the Async CTP).
Asynchronous Programming with Async and Await (C# and Visual Basic)
Async CTP – Task based asynchronous programming for Windows Phone
MVVMLight and Async
I hope this information may help you. MVVM wasn’t simple for me, especially because I was not much into .NET and DataBinding. So the curve at the beginning was very steep. But once you understand the principles it REALLY makes your life easier 😉