Monthly Archives: November 2009

Making Your Win7 JumpLists Trigger on the Same Application Instance

As promised in my previous post, I’ll be talking about how to create jumplist integrated single instance applications, much like how Outlook, Windows Media Player.

At first, i thought it would need to involve a bit of windows messaging but then I found this blog post that details how to create single instance WPF applications. I rewrote our sample from the previous post to adopt this model. I based my code on the post so I recommend reading that post first to have a better understanding of how we are creating this single instance application.

Explaining a bit what is going on here, we basically have 3 major classes:

image

The SingleInstanceManager, as the name implies, manages the instances of your application. If you take a look at the code, basically it creates an instance of your application if it is an initial startup. When a next instance is triggered to start up, it activates the the MainWindow through Win7Application and processes any arguments passed in the triggering of the subsequent instances.

Win7Application is basically our replacement for App.xaml because we want to be able to control when we activate the MainWindow of our application. We also process any arguments of our application in this class.

  public void ProcessArgs(string[] args, bool isFirstInstance)
        {
            if (args.Length > 0)
            {
                if (isFirstInstance)
                    AppWindow.initState = args[0];
                else
                    AppWindow.updateState(args[0]);
            }
            
        }

If you look at the ProcessArgs method in the sample attached, we’re basically setting an initial state (initState) property to the value of the argument as this is expected from the jumplists we have defined in my previous post. If this is not the first instance, we call the updateState() method which I have exposed as a public method in the MainWindow class. 

These are basically the changes I made to MainWindow.xaml.cs:

        private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

            WindowJumpList = JumpList.CreateJumpList();
            initializeJumplist();
            if (initState != null)
                updateState(initState);
          
        }
        public  void updateState(string state)
        {
            ExtendedVisualStateManager.GoToElementState(LayoutRoot, state, true);
            
        }

You can download the source here:

Managing WPF Screens with Windows 7 Jumplists and the Visual State Manager

We’re having our Singapore launch of Windows 7 this November and in honor of that, I’ll be devoting my upcoming (and long overdue) TechFridays to everything Windows 7. For this post, I’ll be talking about how you can use the Visual State Manager for WPF to manage the different screen states in your application in conjunction with your Windows 7 Jumplists.

Before we start, make sure you have the following downloaded:

1. WPF Toolkit – This enables VSM support in current WPF projects

2. http://code.msdn.microsoft.com/WindowsAPICodePack – provides managed wrappers that allow you to easily incorporate Windows 7 JumpLists into your application

* make sure you add references to the above binaries and your set to go.

If you’re not familiar with the Visual State Manager, you can read http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-visual-state-manager-overview.aspx for a brief overview.

What I have is an application with several screens grouped into panels. I also have a visual state group “ScreenStates” where i have 3 states, one for each screen.

image

This should make for easier screen management. You can make use of Blend behaviors to activate these states on the click of the corresponding navigation button / link. If you open up the Behaviors tab in the asset library, you’ll find the GoToStateAction that you can click and drag onto the appropriate control that you want to trigger the state activation.

image

In the screen below, I added the behavior to my GalleryButton. On the properties panel, you’ll see that you can set the StateName you want to activate on the trigger, in this case, the GalleryButton’s Click event.

image

Next we we want to enable Windows 7 Jumplists in our application. Windows 7 is all about helping users become more efficient and one of the features that enable this is the jumplist. Below is a screenshot of Outlook’s jumplists. The application basically exposes tasks that would normally take a few clicks to get to so instead of having to spend the time starting the app and clicking the appropriate menus/options, you jump straight into the screen you want.

image

Below is one way you can integrate jumplists into the app we are building. First, you’ll need to define and initialize a few variables we will be needing to manage the jumplist: the Jumplist itself, and the folder where the assembly is executing.

        private JumpList WindowJumpList;
        string executableFolder;

	private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
	{
			// TODO: Add event handler implementation here.
            WindowJumpList = JumpList.CreateJumpList();
            executableFolder = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            initializeJumplist();
            string[] arguments = Environment.GetCommandLineArgs();
            if (arguments.Length > 1)
            {
                ProcessArguments(arguments[1]);
            }
	}

The following code then initializes the jumplist. Note that the tasks are shell links to the application executable, with the state names of our screens as the arguments. This will make it easier later on to process the arguments and activate the appropriate screen.

image

The last method is the ProcessArguments method. There is a slight kink in the VisualStateManager that won’t allow you to “go to state” as you normally would unless the target is a UserControl. In this case, we had to use a work around which i found here

image

when you compile and run your app, you’ll see the following jumplist tasks and will be able to activate the corresponding screens straight from the jumplist.

image

image

You can download and try out the code here:

This version launches a new instance every time you click on a task on the jumplist. In my next post, I’ll post a sample on how to create jumplist enabled single instance WPF applications. Till next time!