简体   繁体   English

使用文件uri启动程序

[英]Launching a program with a file uri

I am following this tutorial: http://blog.pieeatingninjas.be/2016/02/06/displaying-pdf-files-in-a-uwp-app/ This code is not working with a uwp app: 我正在遵循此教程: http ://blog.pieeatingninjas.be/2016/02/06/displaying-pdf-files-in-a-uwp-app/此代码不适用于uwp应用程序:

Windows.System.LauncherOptions options = newWindows.System.LauncherOptions();
options.ContentType = "application/pdf";
string fileUrl = "file:///C:/Users/Name/Documents/FileName.pdf";
await Windows.System.Launcher.LaunchUriAsync(new Uri(fileUrl), options);

Thank you. 谢谢。

We can't use Launcher.LaunchUriAsync(Uri) method to launch a PDF file by specify such a file path. 我们不能使用Launcher.LaunchUriAsync(Uri)方法通过指定这样的文件路径来启动PDF文件。

Ref from Remarks : 备注引用:

You cannot use this method to launch a URI in the local zone. 您不能使用此方法在本地区域中启动URI。 For example, apps cannot use the file:/// protocol to access files on the local computer. 例如,应用程序无法使用file:///协议来访问本地计算机上的文件。 Instead, you must use the Storage APIs to access files. 相反,您必须使用Storage API来访问文件。

So when using your code, LaunchUriAsync method will always return false , it won't work. 因此,在使用代码时, LaunchUriAsync方法将始终返回false ,它将无法工作。

To launch a file in UWP apps, we can use Launcher.LaunchFileAsync methods . 要在UWP应用中启动文件,我们可以使用Launcher.LaunchFileAsync方法

Firstlly, we need get a Windows.Storage.StorageFile object for the file. 首先,我们需要获取文件的Windows.Storage.StorageFile对象。 The Documents folder is a special folder, we can add documentsLibrary capability in app manifest and then use KnownFolders.DocumentsLibrary to get the PDF file in it. Documents文件夹是一个特殊的文件夹,我们可以在应用清单中添加documentsLibrary功能,然后使用KnownFolders.DocumentsLibrary在其中获取PDF文件。 Or use FileOpenPicker to get the file. 或使用FileOpenPicker来获取文件。 For more info, please see File access permissions and Open files and folders with a picker . 有关更多信息,请参见文件访问权限使用选择器打开文件和文件夹

After we get the file object, we can launch the file with s several different options. 获取文件对象后,可以使用几个不同的选项启动文件。 For more info, please see Launch the default app for a file . 有关更多信息,请参阅启动文件的默认应用程序

This is how I did: 这是我做的:

  1. The PDF file is added to the Assets folder and its Build Action is set to "Content". PDF文件已添加到Assets文件夹,并且其Build Action设置为“ Content”。
  2. The code below is used for showing the PDF file with the default app. 以下代码用于显示默认应用程序的PDF文件。

      StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\\MyDoc.pdf"); var result = await Launcher.LaunchFileAsync(file); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM