Improved Jump Lists Post – and a bit more
Well, I've used up all my sick days so tomorrow I'm calling in dead. In the meantime, I thought of posting a review on a couple of things I have learned over the past few months. The first thing I will work on is code to deal with Jump Lists. My first post on the subject was after my first attempt and doing things the old way, in code. Code junkies like me who've been coding since the green screen days of MS-DOS will probably attack a problem in code first. WPF has changed that in that you can achieve a lot of the same results without writing a single line of executable code. XAML is the answer.
Well, I finally worked on a real app that would benefit a lot from jump lists. And this time, I approached it using XAML. The application even had an overlay icon to notify the user when a long running asynchronous operation was in progress.
The first file I edited when trying this in XAML is the Application.xaml (app.xaml for C#). This is because the Jump List belongs to the application.
<JumpList JumpItemsRejected="JumpList_JumpItemsRejected" JumpItemsRemovedByUser="JumpList_JumpItemsRemovedByUser">
<!-- Add ApplicationPath attributes to point to the app you wish to run when your task is clicked.
You can also place an Arguments attribute which can specify command line arguments for the app.
You can also assign an Icon to the task by specifying an IconResourcePath attribute. -->
<JumpTask Title="Calculator" Description="Launch the windows calculator"
IconResourcePath="C:\Windows\System32\Calc.exe" ApplicationPath="C:\Windows\System32\Calc.exe" />
<JumpPath Path="C:\Users\Public\Readme.txt" />
This piece of XAML registers event handlers to deal with the scenarios when a jump item is rejected, or when a user removes it from the list. In the second scenario it is recommended that you skip loading the item the next time your app runs to respect the user's wishes.
It then adds a JumpTask that runs the windows calculator, and a JumpPath. For the jump path to register, you should register your application to open the file type in question.
The overlay icon is very simple too. In this case, I wrote one line of code to remove the icon, and one other to remove it. Here is the XAML for it.
<DrawingImage x:Key="IconOverlay" >
<ImageDrawing ImageSource="Offline.png" Rect="0 0 32 32" />
You place this in the .xaml file for the main window. In this case, the source is a png, but it can be an icon. The code to show and hide the icon in the task bar follows:
' To add the overlay icon.
Me.TaskbarItemInfo.Overlay = CType(Me.Resources("IconOverlay"), ImageSource)
' To remove the overlay icon.
Me.TaskbarItemInfo.Overlay = Nothing
That's it. Here is the same in C#.
// Add the overlay icon.
this.TaskbarItemInfo.Overlay = (ImageSource)this.Resources["IconOverlay"];
// Remove the thing.
this.TaskbarItemInfo.Overlay = null;
That's really all you have to do.
In a production app you'll need to do more however. You may have more than one scenario in which you'd want to show an overlay icon, in this case, you'll need to handle the various events or situations that fit your application. In the case of the jump list, you may want to load your jump items dynamically so you can determine what the user wishes to see or to reflect the overall status of the application. There's no point in having a "Publish Local Data" if there is no local data to publish.
Hope this helps. Happy coding.