2015-01-07 45 views
3

有沒有什麼方法可以使用xamarin表單查看PDF文件而不使用自定義渲染器。如何使用Xamarin Forms查看PDF文件

+1

嘗試'Device.OpenUri()'並傳入PDF的本地URI。這應該在瀏覽器中打開PDF(希望)能夠查看它。 – Krumelur

+0

我已經嘗試過'Device.OpenUri()',它的工作原理 –

+0

感謝您的建議,但我的應用程序要求是在應用程序內部不打開瀏覽器中打開PDF文件。 – Sonu

回答

5

安卓

public void OpenPdf(string filePath) 
{ 
    Android.Net.Uri uri = Android.Net.Uri.Parse("file:///" + filePath); 
    Intent intent = new Intent(Intent.ActionView); 
    intent.SetDataAndType(uri, "application/pdf"); 
    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); 

    try 
    { 
     Xamarin.Forms.Forms.Context.StartActivity(intent); 
    } 
    catch (Exception) 
    { 
     Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View PDF", ToastLength.Short).Show(); 
    } 
} 

的iOS,有點更加複雜和requiries一些額外的課程:

public void OpenPDF(string filePath) 
{ 
    FileInfo fi = new FileInfo(filePath); 

    QLPreviewController previewController = new QLPreviewController(); 
    previewController.DataSource = new PDFPreviewControllerDataSource(fi.FullName, fi.Name); 

    UINavigationController controller = FindNavigationController(); 
    if (controller != null) 
     controller.PresentViewController(previewController, true, null); 
} 

private UINavigationController FindNavigationController() 
{ 
    foreach (var window in UIApplication.SharedApplication.Windows) 
    { 
     if (window.RootViewController.NavigationController != null) 
      return window.RootViewController.NavigationController; 
     else 
     { 
      UINavigationController val = CheckSubs(window.RootViewController.ChildViewControllers); 
      if (val != null) 
       return val; 
     } 
    } 

    return null; 
} 

private UINavigationController CheckSubs(UIViewController[] controllers) 
{ 
    foreach (var controller in controllers) 
    { 
     if (controller.NavigationController != null) 
      return controller.NavigationController; 
     else 
     { 
      UINavigationController val = CheckSubs(controller.ChildViewControllers); 
      if (val != null) 
       return val; 
     } 
    } 
    return null; 
} 

public class PDFItem : QLPreviewItem 
{ 
    string title; 
    string uri; 

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

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

    public override NSUrl ItemUrl 
    { 
     get { return NSUrl.FromFilename(uri); } 
    } 
} 

public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource 
{ 
    string url = ""; 
    string filename = ""; 

    public PDFPreviewControllerDataSource(string url, string filename) 
    { 
     this.url = url; 
     this.filename = filename; 
    } 

    public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index) 
    { 
     return new PDFItem(filename, url); 
    } 

    public override int PreviewItemCount(QLPreviewController controller) 
    { 
     return 1; 
    } 
} 

使用依存服務,讓他們和呼叫根據需要,工作對我非常好。 PDFItem和PDFPreviewControllerDataSource類來自博客,我不能爲我的生活記住名字,但他們不是我的工作。該FindNavigationController可能沒有必要在你的情況下,它可能是簡單:

UISharedApplication.KeyWindow.PresentViewController 

您可以在這裏找到的信息: https://forums.xamarin.com/discussion/25746/how-to-open-pdf-in-xamarin-forms

相關問題