How to handle file activation (Windows Store apps using JavaScript and HTML)

Language: JavaScript and HTML | VB/C#/C++ and XAML
0 out of 2 rated this helpful - Rate this topic

Windows allows an app to register to become the default handler for a certain file type. Both, desktop apps and Windows Store apps can register to be a default file handler. If the user chooses your app as the default handler for a certain file type, your app will be activated every time that type of file is launched.

You should only register for a file type if you expect to handle all file launches for that type of file. If your app only needs to use the file type internally, then you do not need to register to be the default handler. If you do choose to register for a file type it is important that you provide the end user with the functionality that is expected when your app is activated for that file type. For example a picture viewer app may register to display a .jpg file. For more info on file associations see Guidelines and checklist for file types and URIs.

The following steps will show you how to register for a custom file type, .alsdk, and how your app can be activated when the user launches an .alsdk file.

Instructions

Step 1: Specify the extension point in the package manifest

The app receives activation events only for the file extensions listed in the package manifest. Here's how you indicate that your app handles the files with the .alsdk extension.

  1. Double click to open package.appxmanifest in Solution Explorer.

    See Programmatic Identifiers for more details of identifiers used by file associations. Here is a brief description of each of the fields that you may fill in the package manifest:

    FieldDescription

    Content Type

    Specify the MIME content type, such as image/jpeg, for a particular file type.

    Important Note about allowed content types:  Here is an alphabetic list of MIME content types that you cannot enter into the package manifest because they are either reserved or forbidden: application/force-download, application/octet-stream, application/unknown, application/x-msdownload.

    Name

    Choose a name for a group of file types which share the same display name, logo, info tip, and edit flags. Choose a group name that can stay the same across app updates.

    Note  The Name must be in all lower case letters.

    Info Tip

    Specify the info tip for a group of file types. This tool tip text appears when the user hovers on the icon for a file of this type.

    Edit Flags

    Specify the edit flags for a group of file types. The edit flags control how a file is accessed when it is acquired from an untrusted source. The OpenIsSafe flag indicates that the Open verb for the file type can be safely invoked for any downloaded files. AlwaysUnsafe flag indicates that the option to automatically invoke the Open verb for the file type is disabled. The user can override this attribute through the File Type dialog box. Use of this flag means OpenIsSafe is not respected. It prevents the Never ask me check box from being enabled on the security dialog when opening untrusted files of this type.

    Display Name

    Specify the display name for a group of file types. The display name is used to identify the file type in the Set Default Programs on the Control Panel.

    Logo

    Specify the logo that is used to identify the file type on the desktop and in the Set Default Programs on the Control Panel. If no Logo is specified, the application’s small logo is used.

    File type

    Specify the file type to register for, preceded by a period, e.g. “.jpeg”."

     

  2. Select the Declarations tab.
  3. Select File Type Associations from the drop-down list and click Add.
  4. Enter alsdk as the Name.
  5. Enter .alsdk as the File Type.
    Important note:  Here is an alphabetic list of file type names that you cannot enter into the package manifest because they are either reserved or forbidden: Accountpicture-ms, Appx, application, Appref-ms, Bat, Cer, Chm, Cmd, Com, Cpl, crt, dll, drv, Exe, fon, gadget, Hlp, Hta, Inf ,Ins, jse, lnk, Msi, Msp, ocx, pif, Ps1, Reg, Scf, Scr, Shb, Shs, Sys, ttf, url, Vbe, Vbs, Ws, Wsc, Wsf, Wsh.
  6. Enter “images\Icon.png” as the Logo.
  7. Press Ctrl+S to save the change to package.appxmanifest.

This adds an Extension element like this one to the package manifest. The windows.fileTypeAssociation category indicates that the app handles files with the .alsdk extension.


<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
   <Applications>
      <Application Id="AutoLaunch.App">
         <Extensions>
            <Extension Category="windows.fileTypeAssociation">
                <FileTypeAssociation Name="alsdk">
                  <DisplayName>SDK Sample File Type</DisplayName>
                  <Logo>images\logo.png</Logo>
                  <InfoTip>SDK Sample tip </InfoTip>
                  <EditFlags OpenIsSafe="true" />
                  <SupportedFileTypes>
                     <FileType ContentType="image/jpeg">.alsdk</FileType>
                  </SupportedFileTypes>
               </FileTypeAssociation>
            </Extension>
         </Extensions>
      </Application>
   </Applications>
</Package>

Step 2: Add the proper icons

Apps that become the default for a file type have their icons displayed in various places throughout the system. For example these icons are shown in:

  • Windows Explorer ItemsView, context menus, and the Ribbon
  • Default programs Control Panel
  • Item Picker
  • Search results on the Start screen
It’s important that you include the proper icons with your project so that your logo looks great in all of these places. You should include in your image folder 16/32/48/256 px for the small logo and icon sizes . These icons should match the look of the app tile logo with the color plate baked in. You should test your icons on white backgrounds. See the Association launching sample for example icons.

The Solution Explorer with a view of the files in the images folder. There are 16, 32, 48, and 256 pixel versions of both ‘Icon.targetsize’ and ‘smallTile-sdk’

Step 3: Register for the activated event

Register for the activated event to handle file activation.


WinJS.Application.addEventListener("activated", onActivatedHandler, false);

Step 4: Handle the activated event

The activated event handler registered in step 1 receives all activation events. The kind property indicates the type of activation event. This example is set up to handle file activation events.


function onActivatedHandler(eventArgs) {
   if (eventArgs.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.file) 
   {
       // TODO: Handle file activation.

       // The number of files received is eventArgs.detail.files.size
       // The first file is eventArgs.detail.files[0].name
   }
}

Remarks

The files that you receive could come from an untrusted source. You should validate the content of a file before taking action on it. To find more info on input validation see Writing Secure Code.

Complete example

See Association launching sample.

Related topics

Concepts
Default Programs
File Type and Protocol Associations Model
Tasks
How to launch the default app for a file
How to handle protocol activation
Guidelines
Guidelines and checklist for file types and protocols
Reference
Windows.Storage.StorageFile
Windows.UI.WebUI.WebUIFileActivatedEventArgs
WinJS.Application.onactivated

 

 

Build date: 10/26/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.