在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 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
不支持在Windows Phone 8%的< A HREF =http://msdn.microsoft.com/en-us/library/windows/apps/hh701484.aspx相对=nofollow>文档。这将引发称为异常
LaunchUriAsync
isn't supported on Windows Phone 8 per the documentation. It throws an exception if called
您可以使用 Windows.System.Launcher.LaunchFileAsync
推出 StorageFile
。
这代码的工作,例如(assming有一个名为metro.pdf文件
项目,以生成操作
设置为内容
与复制到输出目录
设置为复制如果较新的
)。
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屋!