Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Windows 7 & WPF 4: interacting in code with the Jump List

Windows 7 introduces a new feature known as Jump List. Basically when you right-click an icon on the task bar you get access to a context menu showing a list of common tasks or simply a list of recent files, divided into groups and categories. With WPF 4 and Visual Basic (or C#) 2010 you can interact with the Jump List by writing some lines of code so that you can associate items lists from your application making such lists available to the user.

WPF 4 exposes the System.Windows.Shell namespace offering some classes that allow accessing the Jump List, particularly the JumpList class representing the above described popup menu. Now imagine you want to add a recent files list to the application icon through the Jump List. After creating a WPF project, let's begin telling the application that it needs a reference to the Windows 7's task bar. This is accomplished, on the XAML side, as follows:

    <Window.TaskbarItemInfo>

        <TaskbarItemInfo/>

    </Window.TaskbarItemInfo>

Next we can write the following managed code (take a look at the comments):

Imports System.Windows.Shell

 

Class MainWindow

 

    Private Sub MainWindow_Loaded(ByVal sender As Object,

                                  ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

 

        'a reference to the Jump List for the running application

        Dim myJumpList As New JumpList

        'represents a shortcut to an application

        'which can be the current application or another one

        Dim myJumpTask As New JumpTask

So the JumpList class represents the context menu while JumpTask is an object representing a shortcut to an application, which can be customized for example with a parameter to be passed to the specified executable. Once you create an instance of the JumpList, you simply assign such instance to the current application via the JumpList.SetJumpList shared method, as follows:

        'Assigning the JumpList instance to the running application

        JumpList.SetJumpList(Application.Current, myJumpList)

Next you need to set properties for the JumpTask object and then assign such object to the JumpList, as demonstrated here:

        'Setting the shortcut's properties

        With myJumpTask

            .CustomCategory = "Recent files"

            .Title = "My Sample Document"

            .Description = "A sample document containing custom information"

            .ApplicationPath = "Notepad.exe"

            .Arguments = "My Sample Document.txt"

        End With

 

        'Adds the element to the JumpList

        myJumpList.JumpItems.Add(myJumpTask)

        'Applies edits

        myJumpList.Apply()

 

    End Sub

When you run the application for the first time, the system will store what you created in code so that the element will be available inside the Jump List each time the application runs. In fact, if the system finds that the element already exists, the code will not create it again. Cool! For a better understanding of this concept, let's make some modifications to the code.

The JumpList class also exposes two events, JumpItemsRejected which is raised if Windows 7 does not accept the insertion of new elements into the Jump List and JumpItemsRemovedByUser which is raised when the user removes an item from the Jump List. After the instance declaration of the JumpList let's add the following line:

        AddHandler myJumpList.JumpItemsRemovedByUser,

                   AddressOf myJumpList_JumpItemsRemovedByUser

Next, let's write code for the event handler:

    Private Sub myJumpList_JumpItemsRemovedByUser(ByVal sender As Object,

                                                  ByVal e As JumpItemsRemovedEventArgs)

        MessageBox.Show(CStr(e.RemovedItems.Count))

    End Sub

Now let's add another item to the Jump List:

        Dim myJumpTask2 As New JumpTask

        With myJumpTask2

            .CustomCategory = "Recent files"

            .Title = "My Sample Document2"

            .Description = "A sample document containing custom information"

            .ApplicationPath = "Notepad.exe"

            .Arguments = "My Sample Document2.txt"

        End With

 

        'Aggiunge l'elemento alla JumpList

        myJumpList.JumpItems.Add(myJumpTask2)

If we now run the application and right-click the icon on the task bar, this is what we obtain:

As you can see the Jump List contains the items that we created in code, grouped inside a group named Recent Files. If you just click one of them, the Windows Notepad will run pointing to the specified (fictitious) file name. Of course in the code we could specify a different executable, such as the current application. Now we can right-click one of the items and select Remove from this list. You will notice that the JumpItemsRemovedByUser won't be raised at this time. It will be in fact raised next time you run the application and this will ensure that the code will not create a duplicate of the item itself. The best test can be accomplished by running the application outside of Visual Studio 2010.

In conclusion, WPF 4 makes easier again developing for Windows 7. The code for this blog post is available from the MSDN Code Gallery at this address.

Alessandro

Print | posted on domenica 24 gennaio 2010 16:10 | Filed Under [ Visual Studio 2010 Visual Basic Windows Presentation Foundation ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET