Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 251
eeadev Avatar asked Jul 06 '15 12:07

eeadev


1 Answers

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); }); 
like image 176
Jeremy Thompson Avatar answered Nov 17 '22 08:11

Jeremy Thompson