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

本文介绍了在Windows Store apps中如何使用Windows.System.Launcher来打开文件和链接。详细讲解了通过默认程序、程序列表以及商店搜索程序打开文件的步骤,并提供了相关代码示例。
部署运行你感兴趣的模型镜像

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






代码下载地址:



您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

1. 首先,要获得Win10 WindowsApps文件夹访问权限的具体操作方法如下: 步骤: 第一步:设置显示隐藏文件夹,打开C:\Program Files文件夹,右击WindowsApps文件夹,选择“属性”,在安全里面选择“高级” 第二步:更改所有者,为Everyone, 确定。 第三步:最后会出现“Windows安全”窗口,等待系统设置好安全信息就可以了。 2. 查:通过系统内置的Windows PowerShellI命令,尝试解决Win10应用商店消失的问题。一、在开始菜单,右键打开Windows PowerShell(管理员),在Windows PowerShell(管理员)页面,输入以下命令,获取您电脑所安装的Windows10内置程序的版本:Get-AppxPackage -allusers 检查是否有附件中安装包, 没有的话,下载附件,解压到WindowsApps文件夹: Name : Microsoft.WindowsStore Publisher : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US Architecture : X64 ResourceId : Version : 11811.1001.27.0 PackageFullName : Microsoft.WindowsStore_11811.1001.27.0_x64__8wekyb3d8bbwe InstallLocation : C:\Program Files\WindowsApps\Microsoft.WindowsStore_11811.1001.27.0_x64__8wekyb3d8bbwe IsFramework : False PackageFamilyName : Microsoft.WindowsStore_8wekyb3d8bbwe PublisherId : 8wekyb3d8bbwe PackageUserInformation : {S-1-5-21-457848557-3406337263-1732915882-1001 [ZunPC]: Installed} IsResourcePackage : False IsBundle : False IsDevelopmentMode : False NonRemovable : False Dependencies : {Microsoft.NET.Native.Framework.1.7_1.7.27413.0_x64__8wekyb3d8bbwe, Microsoft.NET.Native.Runti me.1.7_1.7.25531.0_x64__8wekyb3d8bbwe, Microsoft.VCLibs.140.00_14.0.27323.0_x64__8wekyb3d8bbwe , Microsoft.WindowsStore_11811.1001.27.0_neutral_split.scale-100_8wekyb3d8bbwe...} IsPartiallyStaged : False SignatureKind : Store Status : Ok 3. 安装依存的运行库Microsoft.NET.Native.Framework.1.7 和 Microsoft.VCLibs.140.00_14.0.27323.0_x64: 在Windows PowerShell(管理员)页面,输入以下命令,安装: Add-appxpackage -register "C:\ProgramFiles\WindowsApps\Microsoft.NET.Native.Framework.1.7_1.7.27413.0_x64__8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode (黄色标识的部分,为第二部分中复制的内容) Add-appxpackage -register "C:\ProgramFiles\WindowsApps\Microsoft.NET.Native.Runtime.1.7_1.7.25531.0_x64__8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode Add-appxpackage -register "C:\ProgramFiles\Microsoft.VCLibs.140.00_14.0.27323.0_x64__8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode Add-appxpackage -register "C:\ProgramFiles\WindowsApps\Microsoft.NET.Native.Runtime.1.7_1.7.25531.0_x64__8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode 4. 安装Microsoft.WindowsStore APP Add-appxpackage -register "C:\ProgramFiles\WindowsApps\Microsoft.WindowsStore_11811.1001.27.0_x64__8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode Add-appxpackage -register "C:\ProgramFiles\WindowsApps\Microsoft.WindowsStore_11811.1001.27.0_neutral_split.scale-100_8wekyb3d8bbwe\appxmanifest.xml" -disabledevelopmentmode 即可在开始菜单找到应用商店的图标 (本例采用Windows10.17763.437的应用商店安装包,其他版本也可采用安装)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值