如何从 Xamarin UWP 项目在浏览器中打开 .pdf 文件? [英] How can I open a .pdf file in the browser from a Xamarin UWP project?
问题描述
我有一个 Xamarin 项目,我从头开始生成一个 .pdf 文件并将其保存在我的本地存储中.这工作得很好,可以找到它并在我保存它的磁盘中打开它.但是,我需要在以编程方式创建后立即打开 .pdf 文件.
我已经尝试过使用 Process 和 ProcessStartInfo 的不同变体,但这些只会引发诸如System.ComponentModel.Win32Exception: '系统找不到指定的文件'"和'System.PlatformNotSupportedException'"之类的错误.
这基本上是我尝试使用 Process 打开的路径.
<预><代码>var p = Process.Start(@"cmd.exe", "/c start " + @"P:\\Receiving Inspection\\Inspection Reports\\" + timestamp + ".pdf");我也尝试过使用一些变体的 ProcessStartInfo,但我一遍又一遍地遇到相同的错误.
var p = new Process();p.StartInfo = new ProcessStartInfo(@"'P:\\Receiving Inspection\\Inspection Reports\\'" + timestamp + ".pdf");p.Start();
更好的方法是使用 LaunchFileAsync
使用浏览器打开文件的方法.您可以创建 FileLauncher
DependencyService
从 xamarin 共享项目调用 uwp LaunchFileAsync
方法.
界面
公共接口 IFileLauncher{任务LaunchFileAsync(string uri);}
实施
[程序集:依赖(typeof(UWPFileLauncher))]命名空间 App14.UWP{公共类 UWPFileLauncher : IFileLauncher{公共异步任务LaunchFileAsync(字符串 uri){var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(uri);布尔成功=假;如果(文件!= null){//设置选项以显示选择器var options = new Windows.System.LauncherOptions();options.DisplayApplicationPicker = true;//启动检索到的文件成功 = 等待 Windows.System.Launcher.LaunchFileAsync(file, options);如果(成功){//文件启动}别的{//文件启动失败}}别的{//不能}返回成功;}}}
使用
private async void Button_Clicked(object sender, EventArgs e){await DependencyService.Get().LaunchFileAsync("D:\\Key.pdf");}
请注意,如果要在uwp中访问D盘或C盘,需要添加broadFileSystemAccess
能力.有关更多信息,请参阅this.>
更新
如果 UWP 文件基于网络,而不是基于本地区域,则可以使用 Xamarin.Essentials
使用浏览器打开文件.并且您必须在清单中指定 privateNetworkClientServer
功能.有关更多信息,请参阅此链接.
I have a Xamarin Project where I generate a .pdf file from scratch and save it in my local storage. This works perfectly fine and can find it and open it in the disk where I saved it. However, I need to open the .pdf file immediately after creation programmatically.
I already tried different variations using Process and ProcessStartInfo but these just throw errors like "System.ComponentModel.Win32Exception: 'The system cannot find the file specified'" and "'System.PlatformNotSupportedException'".
This is basically the path I am trying to open using Process.
var p = Process.Start(@"cmd.exe", "/c start " + @"P:\\Receiving inspection\\Inspection Reports\\" + timestamp + ".pdf");
I also tried ProcessStartInfo using some variations but I'm getting the same errors all over and over.
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"'P:\\Receiving inspection\\Inspection Reports\\'" + timestamp + ".pdf");
p.Start();
The better way is that use LaunchFileAsync
method to open file with browser. You could create FileLauncher
DependencyService
to invoke uwp LaunchFileAsync
method from xamarin share project.
Interface
public interface IFileLauncher
{
Task<bool> LaunchFileAsync(string uri);
}
Implementation
[assembly: Dependency(typeof(UWPFileLauncher))]
namespace App14.UWP
{
public class UWPFileLauncher : IFileLauncher
{
public async Task<bool> LaunchFileAsync(string uri)
{
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(uri);
bool success = false;
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not
}
return success;
}
}
}
Usage
private async void Button_Clicked(object sender, EventArgs e)
{
await DependencyService.Get<IFileLauncher>().LaunchFileAsync("D:\\Key.pdf");
}
Please note if you want to access D or C disk in uwp, you need add broadFileSystemAccess
capability. for more please refer this .
Update
If the UWP files are network based, not local zone based, you could use Xamarin.Essentials
to open file with browser. And you must specify the privateNetworkClientServer
capability in the manifest. For more please refer this link.
这篇关于如何从 Xamarin UWP 项目在浏览器中打开 .pdf 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!