在 Windows Phone 8 上启动 PDF 阅读器 [英] Launching PDF reader on windows phone 8
问题描述
我正在尝试使用以下代码启动 pdf 阅读器,但它不起作用.有人可以帮我吗?
I'm trying to launch pdf reader with the code below but it does not work. Can somebody help me?
private async Task<StorageFile> WriteData(string fileName, byte[] data)
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream s = await file.OpenStreamForWriteAsync())
{
await s.WriteAsync(data, 0, data.Length);
s.Close();
}
return file;
}
private async Task<bool> OpenPdf(StorageFile file)
{
var uri = new Uri(file.Path, UriKind.RelativeOrAbsolute);
bool result = await Windows.System.Launcher.LaunchUriAsync(uri);
return result;
}
private async void FetchPdf() {
// Fetch pdf bytes to network
//....
StorageFile file = await WriteData("test.pdf", data);
if (file != null) {
bool result = await OpenPdf(file);
if (result)
Debug.WriteLine("Success");
else
Debug.WriteLine("Cannot open pdf file.");
}
}
result 始终为 false,因此不显示启动器.
result is always false and so launcher is not presented.
我使用 LaunchUriAsync 是因为 LaunchFileAsync 未在 Windows Phone 上实现.
I used LaunchUriAsync because LaunchFileAsync is not implemented on Windows Phone.
推荐答案
LaunchUriAsync
根据 文档.如果调用它会抛出异常
LaunchUriAsync
isn't supported on Windows Phone 8 per the documentation. It throws an exception if called
您可以使用 Windows.System.Launcher.LaunchFileAsync
来启动 StorageFile
.
You can use Windows.System.Launcher.LaunchFileAsync
to launch a StorageFile
.
例如,此代码有效(假设项目中有一个名为 "metro.pdf"
的文件,其中 Build Action
设置为 Content
>,将 Copy to Output Directory
设置为 Copy if Newer
).
This code works for example (assming there's a file called "metro.pdf"
in the project, with the Build Action
set to Content
, with Copy to Output Directory
set to Copy if Newer
).
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assets = await installedLocation.GetFolderAsync("Assets");
var pdf = await assets.GetFileAsync("metro.pdf");
Windows.System.Launcher.LaunchFileAsync(pdf);
这篇关于在 Windows Phone 8 上启动 PDF 阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!