IT tutorials
 
Technology
 

Windows Phone 8 : Other Ways of Launching Your App (part 2) - Using a File Association

8/13/2013 11:19:08 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Using a File Association

Like the custom protocol, the file association enables you to launch your app from other parts of the phone. The difference is that you are really launching a file, not just the app. You can specify a file extension for your application to handle. You can choose any file extension except for the reserved file extensions (see http://shawnw.me/WPReservedFiles for a complete list of reserved file extensions).

To get started, you must modify the WMAppManifest.xml file as text. File associations use the application manifest file to specify the different file extensions. You can open the WMAppManifest.xml file as text by right-clicking the file and selecting View Code.

Just after the Tokens element, you must add a new element called Extensions. Inside the Extensions element, you will create a FileTypeAssociation element, as shown here:

<Deployment xmlns="..."
            AppPlatformVersion="8.0">
  <App ...>
    ...
    <Tokens>
      ...
    </Tokens>
    <Extensions>
       <FileTypeAssociation TaskID="_default"
                           Name="SAMPLE"
                           NavUriFragment="fileToken=%s">
        <Logos>
          <Logo Size="small"
                IsRelative="true">Assets/Logo33x33.png</Logo>
          <Logo Size="medium"
                IsRelative="true">Assets/Logo69x69.png</Logo>
          <Logo Size="large"
                IsRelative="true">Assets/Logo176x176.png</Logo>
        </Logos>
        <SupportedFileTypes>
          <FileType
            ContentType="application/sample">.sample</FileType>
        </SupportedFileTypes>
      </FileTypeAssociation>
    </Extensions>
    ...
  </App>
</Deployment>

The FileTypeAssociation element has several parts. First is a set of logos that are used in various parts of the operating system. You must specify all three logo sizes and supply them. The SupportedFileTypes element contains an extension name and the related ContentType. If you do not have a standard content type, you can use application/YOURAPPNAME. This means that you can now deal with the files that end in .foo. The FileTypeAssociation element’s NavigationUriFragment and TaskID attributes must both have the same values as is shown in the example.

When your application is launched, it will be executed with a special URI that contains information about the file that was launched.You’ll need to create a UriMapperBase derived class. Unlike the earlier example, though, the URI you’re looking for is /FileTypeAssociation:

public class AssociationMapper : UriMapperBase
{
  public override Uri MapUri(Uri uri)
  {
    var fileUri = uri.ToString();

    if (fileUri.Contains("/FileTypeAssociation"))
    {
      // Get the File Token
      var index = fileUri.IndexOf("fileToken=") + 10;
      var token = fileUri.Substring(index);

      // Create path to the XAML to Show File
      var path = string.Concat("/ShowFile.xaml?token=", token);

      return new Uri(path, UriKind.Relative);
    }
    else
    {
      // Let normal navigation happen
      return uri;
    }
  }
}

As you can see here, the operating system is passing in a query string value called fileToken that contains a token to the file that was launched. In this case we’re just post-pending it to the URI for the page we want to launch when a file is shown.

After you have created this new UriMapperBase derived class, you can register it with the application class as shown earlier in the protocol association example.

Back on the page that will handle the file, you can get that token from the query string and use it to interact with the file:

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);

  var token = NavigationContext.QueryString["token"];

  if (!string.IsNullOrWhiteSpace(token))
  {
    var fileName =
      SharedStorageAccessManager.GetSharedFileName(token);

    IStorageFile file = await
      SharedStorageAccessManager.
        CopySharedFileAsync(ApplicationData.Current.LocalFolder,
          fileName,
          NameCollisionOption.GenerateUniqueName,
          token);

      // ...

  }
}

After you’re in the OnNavigatedTo method of the page that is handling the file association, you can use the SharedStorageAccessManager class to get the name of the file by passing in the token. If you need the contents of the file, you can also use the CopySharedFileAsync to copy the file into a local folder (shown here as the ApplicationData.Current.LocalFolder). The CopyShareFileAsync copies the file into a location from which your application can access it and then returns an IStorageFile object that contains the file. 

 
Others
 
- Windows Phone 8 : Other Ways of Launching Your App (part 1) - Using a Custom Protocol
- Windows Phone 8 : Phone Integration - Live Tiles (part 2)
- Windows Phone 8 : Phone Integration - Live Tiles (part 1)
- Active Directory 2008 : Administering Groups in an Enterprise (part 3) - Understanding Shadow Groups, Default Groups
- Active Directory 2008 : Administering Groups in an Enterprise (part 2) - Delegating the Management of Group Membership
- Active Directory 2008 : Administering Groups in an Enterprise (part 1) - Protecting Groups from Accidental Deletion
- Active Directory 2008 : Automating the Creation and Management of Groups (part 2)
- Active Directory 2008 : Automating the Creation and Management of Groups (part 1)
- Managing Exchange Server 2010 Features for Mobile Devices (part 8) - Understanding and Using WebReady Document Viewing
- Managing Exchange Server 2010 Features for Mobile Devices (part 7) - Understanding and Configuring Remote File Access
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us