无法在Metro应用中启动默认应用 [英] Cannot launch default application in a Metro app

查看:122
本文介绍了无法在Metro应用中启动默认应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,因此Windows 8附带了一个很酷的PDF和XLS阅读器应用程序,称为Reader.当用户单击应用程序内的帮助"按钮时,我要显示一个帮助文档.我的应用程序应使用该文档类型的默认查看器启动PDF文档.

Ok, so Windows 8 comes with a cool PDF and XLS reader app, called Reader. I have a help document that I want displayed when the user clicks the Help button inside my app. My app should launch the PDF document with whatever is the default viewer for that document type.

但是不会.没有错误,没有异常,设置断点不会显示任何信息.我的代码是:

But it won't. There are no errors, no exceptions and setting a breakpoint reveals no information. The code I have is:

<Button x:Name="help" Style="{StaticResource HelpAppBarButtonStyle}" Tag="Help" Click="help_Click_1" />

和:

    private async void help_Click_1(object sender, RoutedEventArgs e)
    {

        try
        {
            Windows.Storage.StorageFile file =
                await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"\Assets\User Guide.pdf");
            await Windows.System.Launcher.LaunchFileAsync(file);
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.Message);
        }
    }

现在,在解决方案资源管理器内部,有一些常用的文件和文件夹.我在根目录中也有一个Assets文件夹,而PDF文档位于此根文件夹中.

Now, inside solution explorer, there are the usual files and folders. I also have an Assets folder inside the root directory, and the PDF document is located inside this root folder.

我不确定为什么这行不通,但是我相信这可能与我在上面的代码中指向文件的方式有关:

I am not sure why this isn't working, but I believe that it may have something to do with how I am pointing to the file in the above code:

await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"\Assets\User Guide.pdf");

如何使用该文档类型的默认应用程序正确显示Assets文件夹中包含的文档?

How do you correctly display a document contained in the Assets folder with using the default application for that document type?

推荐答案

我测试了您的代码,在使其正常工作之前,我必须修复两个问题:

I tested your code and I had to fix two issues before I got it to work:

  1. 确保将pdf文件的Build Action设置为Content.在我的情况下,将文件包含在项目中时将其设置为None.
  2. 您需要删除文件路径Package.Current.InstalledLocation.GetFileAsync(@"Assets\User Guide.pdf")中的起始反斜杠.
  1. Make sure the Build Action for the pdf file is set to Content. It was set to None in my case when I included the file in the project.
  2. You need to delete the starting backslash in the file path: Package.Current.InstalledLocation.GetFileAsync(@"Assets\User Guide.pdf").

更改以上两个选项后,将在默认的PDF查看器中打开文件.但是,即使在此之前,也出现了一个异常来描述发生了什么问题,尽管起初由于您的静音try/catch代码我没有注意到它.

After I changed both of the above the file opened in my default PDF viewer. But even before that an exception was raised describing what went wrong although I didn't notice it at first because of your silent try/catch code.

如果它仍然对您不起作用,则可能与您的计算机配置有关.确保正确设置了pdf文件的默认查看器.

If it still doesn't work for you, it's probably something related to the configuration of your machine. Make sure the default viewer for pdf files is set up correctly.

这篇关于无法在Metro应用中启动默认应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆