Launching a File

,

Any app can request that the OS launch a file. Launching a file is achieved using the Windows.System.Launcher class. The following excerpt from the MainPageViewModel retrieves a file stored in the app’s private storage area and launches it using the Launcher’s LaunchFileAsync method:

async Task LaunchFile()
{
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    StorageFile file = await folder.GetFileAsync("Sample.unl");
    Launcher.LaunchFileAsync(file);
}

The path to the file must exist. If the file does not exist, a FileNotFoundException is raised.

The MainPageViewModel in the WPUnleashed.AppThatLaunchesACustomFileType project saves a sample file with the .unl extension (see Listing 30.4). The storage API is used to create a new file. A message is constructed, converted to bytes, and then written to the file using the Stream WriteAsync method.

LISTING 30.4. MainPageViewModel.SaveSampleFile Method


async void SaveSampleFile()
{
    string assemblyName = Assembly.GetExecutingAssembly().FullName;
    assemblyName = assemblyName.Substring(
        0, assemblyName.IndexOf(",", StringComparison.Ordinal));

    string fileContent = string.Format(
        "This text was saved to a file at {0} from the assembly: {1}.",
        DateTime.Now, assemblyName);

    IStorageFolder folder = ApplicationData.Current.LocalFolder;
    IStorageFile file = await folder.CreateFileAsync(
                                fileName, CreationCollisionOption.ReplaceExisting);

    using (Stream stream = await file.OpenStreamForWriteAsync())
    {
        byte[] content = Encoding.UTF8.GetBytes(fileContent);
        await stream.WriteAsync(content, 0, content.Length);
    }
}



Note

The sample’s MainPageViewModel class is a linked file that is shared by both projects to reduce code duplication.


An ICommand named launchFileCommand calls the LaunchFile method, like so:

launchFileCommand = new DelegateCommand(obj => LaunchFile());

A button in the view is bound to the LaunchFileCommand property, as shown:

<Button Content="Launch File" Command="{Binding LaunchFileCommand}" />

Tapping the button causes the file to be loaded by the LaunchFile method, and the app is deactivated. The OS identifies the app that is registered to handle the .unl file extension and launches the WPUnleashed.FileAndProtocolAssociations app, which then navigates to the FileView.xaml page to display the file contents (see Figure 30.1).

Image

FIGURE 30.1 The file content is displayed on the FileView.xaml page.


Note

Launching a file or a protocol from within the app that has the file or protocol registration produces slightly different life-cycle behavior. When launching from a different app, the app is deactivated (its Deactivated event is raised). When launching from the same app, the app is closed (its Closed event is raised) and then reopened to handle the launch request.


Reserved File Extensions

A small number of file extensions cannot be associated with your app. Table 30.2 lists the file extensions that are reserved for built-in apps, such as Office, and Table 30.3 lists file extensions that are reserved by the Windows Phone OS.

TABLE 30.2. File Types Reserved for Built-In Apps

Image

TABLE 30.3. File Types Reserved for the Windows Phone OS

Image
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.234.214