System.Runtime.InteropServices.COMException when launching a pdf file on Windows Phone

╄→гoц情女王★ 提交于 2019-12-08 15:50:05

问题


I am trying to open a pdf file using the below working code I previously used on another app,

but this time I am getting System.Runtime.InteropServices.COMException when the flow hits this line: Windows.System.Launcher.LaunchFileAsync(pdffile);

What is the meaning of this exception and how to get rid of it?

Please note that without caring about this exception (disabling it),

the file still cannot be opened.

Please note: the file exists in my isolated folder (checked with wpowertool),

I tried with 2 different files so it shouldnt be a matter of file corruption.

    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); 
    }

this MSDN post belongs to me. Yes, the file is installed and I have acrobat reader.

Please note that this C# code is a phonegap/cordova plugin which is called via javascript in my hybrid application.


回答1:


Pay attention to the remarks in this 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).


Huge ups to the OP for sharing the solution. The following code should help others get around this issue:
Deployment.Current.Dispatcher.BeginInvoke(() => { asyncOpenFile(options); }); 


来源:https://stackoverflow.com/questions/31246020/system-runtime-interopservices-comexception-when-launching-a-pdf-file-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!