This blog post is part of my Windows 8 metro-style application declarations series. Read more about declarations in the introductory post about application manifest.
File Type Associations Declaration is very similar to Protocol Declaration. Similar in a way that it defines an OS-wide declaration that’s used to launch your application. What this declaration does should be familiar to most Windows users as it’s been a part of the system from the very beginning.
Every file extension can be associated with a particular application that can handle the content of such file. Most text editors, for example, know how to handle files with .txt extension , file formats like .jpg, .png, and .bmp, are handled by graphics applications like Paint or PhotoShop, etc.
File Type Associations Declaration lets you declare specific file extension(s) that your application will be able to handle. It can be an existing, well-known extension such as .txt, or you can come up with your very own extension and/or file format and register that. For this example, I’ll declare two extensions – .jpg (well-known graphics format extension, and .jpgx – a made up extension that will serve just as an example for this blog post) - my sample application will display the same photo viewer for both extensions.
Setting up the photo viewer page
I’ve created a new blank page in my application and put the Image control on it. That’ll serve as my simple photo viewer.
I’ve also added a method that will help me set the image. The method takes a parameter of type StorageFile, which is a new class in windows runtime that takes care of wrapping file info and does basic file manipulation.
public async void SetPhoto(StorageFile storageFile)
BitmapImage image = new BitmapImage();
IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read);
Viewer.Source = image;
The above snippet shows a very basic way to asynchronously open a file. The stream is set as an image source after the file contents are fully read.
Similar to Protocol example from my last post, the application is notified when user launches a file with associated extension, but the API behaves a bit differently here. The OnProtocolActivated method is already built into the Application class and you only have to override it in App class derivate to handle the activation. This behavior is most likely to change in one of the future versions of WinRT, but I currently have no idea whether we’ll get a separate method (OnXXXActivated) for each activation kind, or will there be a single method OnActivated, that will provide the activation kind through one of its parameters. Currently, there’s a weird mixture of both approaches.
Here’s how the overridden OnFileActivated method looks like:
protected override void OnFileActivated(FileActivatedEventArgs args)
PhotoView page = new PhotoView();
page.SetPhoto(args.Files as StorageFile);
Window.Current.Content = page;
Interesting things to note about the provided FileActivatedEventArgs class are:
- args.Files contains a collection of files that were used in activation. In this example, I’m only using the first one.
- args.PreviousExecutionState will tell you the state the application was in before activation. It can be either NotRunning, Running, Suspended and Terminated.
- args.Verb is the action that was used for activating the app. Should be set to Open.
And that’s it for the code. The last thing is setting up the Declaration in the application manifest.
This is the first part of the File Type Association Declaration Page. DisplayName is a localizable string that will be displayed to the user as associated file a handler and the graphics can be changed by specifying a new Logo. Info Tip should contain the text that will be displayed in a tooltip when user hovers a mouse over the application’s associated file extension. Name is a unique name of the association.
Edit Flags specify what type of security prompt will be displayed to the user when she launches the associated file. OpenIsSafe will show a standard warning dialog, while AlwaysUnsafe will display a security warning dialog.
The second part of the Declaration configuration lets you set the supported file types. You can add as many file extensions as you want your application to handle and you can specify an optional content type for each of them. In the above example, extensions .jpg and .jpgx are registered.
Application Settings section lets you configure application’s entry point settings.
Running the application
Run (or just deploy) your application, then launch a .jpg file of your choice that’s on your disk. The following dialog should appear, asking you to select the default application that will handle that file extension in the future:
After selecting your app as the default, it should be launched (or activated if already running) and display the image.
Renaming a .jpg file to (previously unassociated extension) .jpgx and launching that, for example, will give you the following dialog:
You can check (and change) the default application for each of the file extensions through the Control Panel. Both .jpg and .jpgx are associated with my demo application:
Congratulations, you’ve just created your first file extension handling Metro application