Last week with Matias Woloski and Ezequiel Jadib we created a demo for a screencast (in Spanish) that shows how to take advantage of some Windows 7 features in a WPF application, improving the user experience (after all that is one of the main goals of WPF). You can find the code for the clean application (without Windows 7 functionality) here, and the fully functional application over here. In this and future posts I will go over the relevant additions to the application, so you can see how easy it is to take advantage of Windows 7 features in your existing application. At the end of each post I will provide the outcome solution.
Demo Scenario
The application shows a Snow Board Shop that allows to Ship Orders of different Snow Boards. It also provides support for temporarily connected scenarios using the Disconnected Service Agent Application Block from the Smart Client Software Factory.
The demo shows the following interaction with Windows 7:
- How to: Show connectivity status in the Windows 7 Taskbar [in this post]
- How to: Show progress while shipping orders [in this post]
- How to: Show a thumbnail of the application [coming soon]
- How to: Use Thumbnail buttons [coming soon]
- How to: Open a new application with the Jumplist [coming soon]
Implementation Details
- The demo leverages Windows 7 functionalities through the Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll. Downloaded from here (WindowsAPICodePack).
How to: Show connectivity status in the Taskbar
What we wanted to do is show whether the application is connected to the internet (which would in turn enable it to ship orders), without having the window open. What we decided to do is show an icon in the application's taskbar that reflect the connectivity status. To do this, we must first add a reference to System.Drawing.dll and to both assemblies mentioned above (located in the Lib folder). Then, update the UpdateConnectionStatus method to change the icon based on the status:
private void UpdateConnectionStatus(bool isConnected) { string status; Icon icon; if (isConnected) { status = "Online"; icon = new Icon("images\Available.ico"); StatusLabel.Style = (Style)FindResource("StatusLabelOnline"); StatusLabel.Content = status; } else { status = "Offline"; icon = new Icon("images\Offline.ico"); StatusLabel.Style = (Style)FindResource("StatusLabelOffline"); StatusLabel.Content = status; } Taskbar.OverlayImage.Icon = icon; Taskbar.OverlayImage.Text = status; }
If you run the application now and connect/disconnect from the Internet, you will notice the icon in the Taskbar changing.
How to: Show progress while shipping orders
Now that we are able to show connectivity, and are working in a scenario with Offline capabilities it would be good to show the progress of the orders being dispatched without having the application's window open. That's when the Windows 7 taskbar comes to the rescue. Our goal is to show progress, by increasing the progress value as less orders are pending to dispatch. All we have to do is update the handlers of the RequestDispatched and RequestEnqueued events with code like this:
private delegate void OnRequestEnqueuedDelegate(object sender, RequestEnqueuedEventArgs e); private void OnRequestEnqueued(object sender, RequestEnqueuedEventArgs e) { this.UpdateRequestQueue(); if (Dispatcher.Thread != System.Threading.Thread.CurrentThread) { Dispatcher.Invoke(new OnRequestEnqueuedDelegate(OnRequestEnqueued), sender, e); } else { //Start showing progress when the first request is enqueued if (manager.RequestQueue.GetCount() == 1) Taskbar.ProgressBar.State = TaskbarButtonProgressState.Indeterminate; Taskbar.ProgressBar.MaxValue = manager.RequestQueue.GetCount(); Taskbar.ProgressBar.CurrentValue = 0; } } private delegate void OnRequestDispatchedDelegate(object sender, RequestDispatchedEventArgs e); private void OnRequestDispatched(object sender, RequestDispatchedEventArgs e) { this.UpdateRequestQueue(); if (Dispatcher.Thread != System.Threading.Thread.CurrentThread) { Dispatcher.Invoke(new OnRequestDispatchedDelegate(OnRequestDispatched), sender, e); } else { //Stop showing progress when there are no more requests if (manager.RequestQueue.GetCount() == 0) { Taskbar.ProgressBar.State = TaskbarButtonProgressState.NoProgress; } else { Taskbar.ProgressBar.State = TaskbarButtonProgressState.Normal; Taskbar.ProgressBar.CurrentValue += 1; } } }
To be able to see how this works, I would recommend disconnecting from the Internet (so no orders are dispatched) and enqueuing about 20 orders. Then connect, and you will be able to see the progress bar increasing as orders are dispatched.
Outcome
Disclaimer
This code is provided “AS IS” with no warranties, and confers no rights.
Download
You can get source code here: SnowShopPart1.zip.
Stay tuned because I will blog about all the other features soon :) .
EDIT August 31st 2009: Blogged part 2.