Created
May 3, 2017 19:41
-
-
Save julesx/ad1c129b1e995f67c2f90792e40372f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DocumentViewer : IDocumentViewer | |
{ | |
public void ShowDocumentFile(string filepath, string mimeType) | |
{ | |
var uri = global::Android.Net.Uri.Parse("file://" + filepath); | |
var intent = new Intent(Intent.ActionView); | |
intent.SetDataAndType(uri, mimeType); | |
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); | |
Forms.Context.StartActivity(intent); | |
} | |
public DocumentViewer() | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var documentViewer = DependencyService.Get<IDocumentViewer>(); | |
var mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; | |
if (Path.GetExtension(filename).ToLower() == ".pdf") | |
mimeType = "application/pdf"; | |
else if (Path.GetExtension(filename).ToLower() == ".doc") | |
mimeType = "application/msword"; | |
documentViewer.ShowDocumentFile(filepath, mimeType); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDocumentViewer | |
{ | |
void ShowDocumentFile(string filepath, string mimeType); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DocumentViewer : IDocumentViewer | |
{ | |
public DocumentViewer() | |
{ | |
} | |
public void ShowDocumentFile(string filepath, string mimeType) | |
{ | |
var fileinfo = new FileInfo(filepath); | |
var previewController = new QLPreviewController | |
{ | |
DataSource = new PreviewControllerDataSource(fileinfo.FullName, fileinfo.Name) | |
}; | |
var window = UIApplication.SharedApplication.KeyWindow; | |
var vc = window.RootViewController; | |
while (vc.PresentedViewController != null) | |
{ | |
vc = vc.PresentedViewController; | |
} | |
vc?.PresentViewController(previewController, true, null); | |
} | |
} | |
public class DocumentItem : QLPreviewItem | |
{ | |
private readonly string _uri; | |
public DocumentItem(string title, string uri) | |
{ | |
ItemTitle = title; | |
_uri = uri; | |
} | |
public override string ItemTitle { get; } | |
public override NSUrl ItemUrl => NSUrl.FromFilename(_uri); | |
} | |
public class PreviewControllerDataSource : QLPreviewControllerDataSource | |
{ | |
private readonly string _url; | |
private readonly string _filename; | |
public PreviewControllerDataSource(string url, string filename) | |
{ | |
_url = url; | |
_filename = filename; | |
} | |
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) | |
{ | |
return new DocumentItem(_filename, _url); | |
} | |
public override nint PreviewItemCount(QLPreviewController controller) | |
{ | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment