您的位置:首页 > 移动开发

Windows Store apps开发[47]使用默认程序打开文件

2012-10-29 23:08 615 查看



注:本文由BeyondVincent(破船)原创首发

转载请注明出处:BeyondVincent(破船)@DevDiv.com







更多内容请查看下面的帖子

[DevDiv原创]Windows
8 开发Step by Step



小引

在Windows 8中,有时候,我们电脑中的文件,需要启动别的程序来打开,或者有时候我们需要通过浏览器打开某个链接,这时候我们就需要用到Windows.System.Launcher。今天我就通过代码示例,来介绍如何打开文件或者链接。

本文参考了:

http://msdn.microsoft.com/zh-cn/library/windows/apps/windows.system.launcher

打开文件

通过默认程序打开文件

private async void DefaultLaunch(object sender, RoutedEventArgs e)
{
    // Path to the file in the app package to launch
    string imageFile = @"data\[DevDiv翻译]Metro Revealed_ Building Windows 8 apps with XAML and C#中文翻译合集_2012_09_03.pdf";

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

    if (file != null)
    {
        // Launch the retrieved file
        var success = await Windows.System.Launcher.LaunchFileAsync(file);

        if (success)
        {
            // File launched
        }
        else
        {
            // File launch failed
        }
    }
    else
    {
        // Could not find file
    }
}


运行效果:









通过程序列表打开文件

private async void DisplayApplicationPicker(object sender, RoutedEventArgs e)
{
    // Path to the file in the app package to launch
    string imageFile = @"data\7.jpg";

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

    if (file != null)
    {
        // Set the option to show the picker
        var options = new Windows.System.LauncherOptions();
        options.DisplayApplicationPicker = true;

        // Launch the retrieved file
        bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
        if (success)
        {
            // File launched
        }
        else
        {
            // File launch failed
        }
    }
    else
    {
        // Could not find file
    }
}


运行效果





去商店搜索对应的程序来打开文件

private async void RecommendedApp(object sender, RoutedEventArgs e)
{
    // Path to the file in the app package to launch
    string imageFile = @"data\1.BeyondVincent";

    // Get the image file from the package's image directory
    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

    if (file != null)
    {
        // Set the recommended app
        var options = new Windows.System.LauncherOptions();

        // 设置为应用商店中要推荐的应用的程序包系列名称
        options.PreferredApplicationPackageFamilyName = "BeyondVincent格式文件程序";

        // 设置为该应用的名称
        options.PreferredApplicationDisplayName = "BV_Launcher";

        // Launch the retrieved file pass in the recommended app 
        // in case the user has no apps installed to handle the file
        bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
        if (success)
        {
            // File launched
        }
        else
        {
            // File launch failed
        }
    }
    else
    {
        // Could not find file
    }
}


运行效果





打开链接

private async void OpenDevDiv(object sender, RoutedEventArgs e)
{
    // The URI to launch
    string uriToLaunch = @"http://www.DevDiv.com";

    // Create a Uri object from a URI string 
    var uri = new Uri(uriToLaunch);

    // Launch the URI
    var success = await Windows.System.Launcher.LaunchUriAsync(uri);

    if (success)
    {
        // URI launched
    }
    else
    {
        // URI launch failed
    }
}






代码下载地址:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: