如何在HoloLens UWP的相关应用程序中启动PDF文件?

时间:2017-01-16 10:07:58

标签: c# uwp hololens

我试图打开一个PDF文件,打包在应用程序文件夹("资产")中,在HoloLens(C#)应用程序中。

        string imageFile = @"Assets\test.jpg";

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

        Debug.WriteLine("Opening file in path :" + file.Path);

        if (file != null)
        {
            Debug.WriteLine("File found\nLaunching file...");

            var options = new Windows.System.LauncherOptions();
            options.DisplayApplicationPicker = true;
            // Launch the retrieved file
            var success = await Windows.System.Launcher.LaunchFileAsync(file);

            if (success)
            {
                Debug.WriteLine("File launched successfully");
                // File launched
            }
            else
            {
                Debug.WriteLine("File launch failed");
                // File launch failed
            }
        }
        else
        {
            Debug.WriteLine("Could not find file");
            // Could not find file
        }

出现Access is Denied错误。

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at SchneiderDemoLibrary.HoloHelper.<DefaultLaunch>d__3.MoveNext() Exception caught.

有人可以帮我解决这个问题吗?我只是想用与文件类型相关联的相应应用程序打开一些文件。

1 个答案:

答案 0 :(得分:1)

  1. 确认该文件已包含在您的项目中,并且Build Action设置为&#34; Content&#34;。

  2. 切换为通过IStorageFile电话检索StorageFile.GetFileFromApplicationUriAsync引用。

  3. 以下代码适用于常规UWP应用程序:

    private async void OpenPdfButton_Click(object sender, RoutedEventArgs e)
    {
        var file = await StorageFile.GetFileFromApplicationUriAsync
        (
            new Uri("ms-appx:///Content/output.pdf")
        );
    
        await Launcher.LaunchFileAsync(file);
    }