我正在尝试使用下面的代码启动pdf阅读器,但它不起作用。有人可以帮帮我吗?
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.");
}
}
结果始终为false,因此不会显示启动器。
我使用了LaunchUriAsync,因为在Windows Phone上没有实现LaunchFileAsync。
答案 0 :(得分:4)
LaunchUriAsync
。如果调用,它会抛出异常
您可以使用Windows.System.Launcher.LaunchFileAsync
发布StorageFile
。
此代码适用于示例(确定项目中有一个名为"metro.pdf"
的文件,Build Action
设置为Content
,Copy to Output Directory
设置为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);
答案 1 :(得分:0)
调用API并将字节数组保存到文件
public static async void WriteDataToIsolatedStorageFile(string fileName, byte[] data)
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile(fileName, FileMode.Create))
{
if ((data != null) && (data.Length > 0))
{
await stream.WriteAsync(data, 0, data.Length);
}
}
}
}
使用
在pdf阅读器中打开文件 private async void StartExternalPDFApp()
{
StorageFolder localFolder = await FileManager.FindDirectory(FileManager.RelativeStorageDirectoryLocalStorage);
StorageFile storageFile = await localFolder.GetFileAsync(PdfFileName);
await Windows.System.Launcher.LaunchFileAsync(storageFile);
}
localFolder是Windows.Storage.ApplicationData.Current.LocalFolder
答案 2 :(得分:0)
将anyFile.pdf放在Assets文件夹中,然后对Content进行构建操作,然后只需将函数Async ...然后Put&#34; await&#34;在Windows.System.Launcher.LaunchFileAsync(pdf)之前; 它对我来说很好。尼斯。 看到这个。
private async void privacyPolicy_Click(object sender, EventArgs e)
{
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assets = await installedLocation.GetFolderAsync("Assets");
var pdf = await assets.GetFileAsync("PrivacyPolicy.pdf");
await Windows.System.Launcher.LaunchFileAsync(pdf);
}