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

本文介绍了在Windows Store apps中如何使用Windows.System.Launcher来打开文件和链接。详细讲解了通过默认程序、程序列表以及商店搜索程序打开文件的步骤,并提供了相关代码示例。
摘要由CSDN通过智能技术生成

注:本文由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
    }
}






代码下载地址:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值