Cannot launch default application in a Metro app

我的未来我决定 提交于 2019-12-25 06:44:08

问题


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" />

and:

    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);
        }
    }

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");

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


回答1:


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

  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").

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.

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.



来源:https://stackoverflow.com/questions/14570276/cannot-launch-default-application-in-a-metro-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!