Monotouch - Issue with QLPreviewController

2019-07-23 11:39发布

问题:

I am trying to use QLPreviewController to see a PDF file and send it, but I have an issue with the action button after previewing the PDF document.

When I press the action button (at the top right) app crashes and I get: "Unhandled managed exception: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported. (MonoTouch.Foundation.MonoTouchException)"

I did some research and it seams that this issue may occur if you download a file from the internet or if the file type is not "file:// .......... ". My NSUrl is on that format so I dont know why I have this error.

Anybody has any idea?

Thanks

Here is my code to call the Controller:

QLPreviewController previewController= new QLPreviewController();             

previewController.DataSource=new MyQLPreviewControllerDataSource();     

this.PresentViewController(previewController,true, null);

This is my code for the DataSource:

public class MyQLPreviewControllerDataSource : QLPreviewControllerDataSource { public     override int PreviewItemCount (QLPreviewController controller) {

    return 1;
}

public override QLPreviewItem GetPreviewItem (QLPreviewController controller, int index)
{

    string fileName = @"example.pdf";
    var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
    var library = Path.Combine (documents,fileName);
    NSUrl url = NSUrl.FromFilename (library);
    return new QlItem ("Title", url);
}
}

This is my code for the item:

public class QlItem : QLPreviewItem { string _title; Uri _uri;

public QlItem (string title, Uri uri) 
{ 
    this._title = title; 
    this._uri = uri; 
} 

public override string ItemTitle { 
    get { return _title; } 
} 

public override NSUrl ItemUrl { 
    get { return _uri; } 
} 

}

回答1:

Your QlItem class is casting the original NSUrl into a Uri before casting it back into a NSUrl and something is getting lost along the way.

It should look more like:

    public class QlItem : QLPreviewItem 
    { 
        string title; 
        NSUrl uri; 

        public QlItem(string title, NSUrl uri) 
        { 
            this.title = title; 
            this.uri = uri; 
        } 

        public override string ItemTitle { 
            get { return title; } 
        } 

        public override NSUrl ItemUrl { 
            get { return uri; } 
        } 
    }