System.Runtime.InteropServices.COMException 在 Windows Phone 上启动 pdf 文件时
System.Runtime.InteropServices.COMException when launching a pdf file on Windows Phone
我正在尝试使用我之前在另一个应用程序中使用的以下工作代码打开 pdf 文件,
但是这次当流程到达这一行时我得到 System.Runtime.InteropServices.COMException:Windows.System.Launcher.LaunchFileAsync(pdffile);
此异常的含义是什么以及如何摆脱它?
请注意,在不关心此异常(禁用它)的情况下,
文件仍然无法打开。
请注意:该文件存在于我的隔离文件夹中(使用 wpowertool 检查),
我尝试了 2 个不同的文件,所以这应该不是文件损坏的问题。
public void openFile(string options)
{
System.Diagnostics.Debug.WriteLine("options: " + options);
string optVal = JsonHelper.Deserialize<string[]>(options)[0];
asyncOpen(optVal);
}
public async Task asyncOpen(string filename)
{
filename = filename.Substring(2, filename.Length - 2);
filename = filename.Replace("//", "/").Replace("/", "\");
Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
Debug.WriteLine("local: " + local.Path);
Windows.Storage.StorageFile pdffile = await local.GetFileAsync(filename);
Debug.WriteLine("pdffile: " + pdffile.Name);
//// Launch the pdf file.
Windows.System.Launcher.LaunchFileAsync(pdffile);
}
这个MSDN post是我的。是的,文件已安装,我有 acrobat reader.
请注意,此 C# 代码是 phonegap/cordova plugin,在我的混合应用程序中通过 javascript 调用。
注意这个MSDN Article - Launcher.LaunchFileAsync(IStorageFile)中的备注:
The calling app must be visible to the user when the API is invoked.
This API must be called from an ASTA thread (also known as a UI
thread).
非常感谢 OP 分享解决方案。以下代码应该可以帮助其他人解决此问题:
Deployment.Current.Dispatcher.BeginInvoke(() => { asyncOpenFile(options); });
我正在尝试使用我之前在另一个应用程序中使用的以下工作代码打开 pdf 文件,
但是这次当流程到达这一行时我得到 System.Runtime.InteropServices.COMException:Windows.System.Launcher.LaunchFileAsync(pdffile);
此异常的含义是什么以及如何摆脱它?
请注意,在不关心此异常(禁用它)的情况下,
文件仍然无法打开。
请注意:该文件存在于我的隔离文件夹中(使用 wpowertool 检查),
我尝试了 2 个不同的文件,所以这应该不是文件损坏的问题。
public void openFile(string options)
{
System.Diagnostics.Debug.WriteLine("options: " + options);
string optVal = JsonHelper.Deserialize<string[]>(options)[0];
asyncOpen(optVal);
}
public async Task asyncOpen(string filename)
{
filename = filename.Substring(2, filename.Length - 2);
filename = filename.Replace("//", "/").Replace("/", "\");
Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
Debug.WriteLine("local: " + local.Path);
Windows.Storage.StorageFile pdffile = await local.GetFileAsync(filename);
Debug.WriteLine("pdffile: " + pdffile.Name);
//// Launch the pdf file.
Windows.System.Launcher.LaunchFileAsync(pdffile);
}
这个MSDN post是我的。是的,文件已安装,我有 acrobat reader.
请注意,此 C# 代码是 phonegap/cordova plugin,在我的混合应用程序中通过 javascript 调用。
注意这个MSDN Article - Launcher.LaunchFileAsync(IStorageFile)中的备注:
The calling app must be visible to the user when the API is invoked.
This API must be called from an ASTA thread (also known as a UI thread).
非常感谢 OP 分享解决方案。以下代码应该可以帮助其他人解决此问题:
Deployment.Current.Dispatcher.BeginInvoke(() => { asyncOpenFile(options); });