2016-12-27 91 views
0

为长头衔道歉!如何编写一种方法在C#中打开一个图像,该图像将使用移动设备上的默认图像查看器...?

我是相当新的C#(大概只有2-3个月的具体知识的),我在大学里学习...

我一直在尝试与Xamarin窗体XAML,我想知道我怎么会在代码隐藏中编写一个方法,打开要在Android或iOS的默认图像查看器中打开的点击图像。

我说Android或iOS是因为我在做跨平台的Xamarin Forms PCL。

谢谢:)节日快乐,新年快乐:D!

+0

的,你需要正确的平台特定的代码 –

回答

2

尝试以下代码:

PCL接口:

namespace MyApp.Common.Interfaces 
{ 
    public interface IDataViewer 
    { 
     void showPhoto(string AttachmentName, byte[] AttachmentBytes); 
     string ImageExists(string Filename, byte[] ImageData); 
    } 
} 

平台特定(德罗伊德):

using Android.Content; 
using Android.Webkit; 
using Android.Widget; 
using System; 
using System.IO; 
using MyApp.Common.Interfaces; 
using Xamarin.Forms; 

[assembly: Dependency(typeof(DataViewer))] 
namespace MyApp.Droid.Common 
{ 
    public class DataViewer : IDataViewer 
    { 
     public void showPhoto(string AttachmentName, byte[] AttachmentBytes) 
     { 
      string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path; 
      var FileName = AttachmentName; 
      Java.IO.File file = new Java.IO.File(dirPath, FileName); 

      if (!file.Exists()) 
      { 
       var filename = Path.Combine(dirPath, AttachmentName); 
       File.WriteAllBytes(filename, AttachmentBytes); 
      } 

      Device.BeginInvokeOnMainThread(() => 
      { 
       //var oDir = Xamarin.Forms.Forms.Context.FilesDir.AbsolutePath; 
       Android.Net.Uri uri = Android.Net.Uri.FromFile(file); 
       Intent intent = new Intent(Intent.ActionView); 
       String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl((string)uri).ToLower()); 
       intent.SetDataAndType(uri, mimeType); 

       intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); 

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

     public string ImageExists(string FileName, byte[] Imagedata) 
     { 
      string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path; 

      Java.IO.File file = new Java.IO.File(dirPath, FileName); 

      if (!file.Exists()) 
      { 
       var filename = Path.Combine(dirPath, FileName); 
       File.WriteAllBytes(filename, Imagedata); 
       return filename; 
      } 
      else 
      { 
       var filename = Path.Combine(dirPath, FileName); 
       return filename; 
      } 
     } 
    } 
} 

平台特定(IOS):

using Foundation; 
using QuickLook; 
using System; 
using System.IO; 
using UIKit; 
using MyApp.Common.Interfaces; 

[assembly: Dependency(typeof(DataViewer))] 
namespace MyApp.iOS.Common 
{ 
    public class DataViewer : IDataViewer 
    { 
     public void showPhoto(string AttachmentName, byte[] AttachmentBytes) 
     { 
      var FileName = AttachmentName; 
      string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

      var filename = Path.Combine(dirPath, FileName); 
      FileInfo fi = new FileInfo(filename); 

      if (!NSFileManager.DefaultManager.FileExists(filename)) 
      { 
       Stream stream = new MemoryStream(AttachmentBytes); 
       NSData imgData = NSData.FromStream(stream); 
       NSError err; 
       imgData.Save(filename, false, out err); 
      } 

      Xamarin.Forms.Device.BeginInvokeOnMainThread(() => 
      { 
       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 string ImageExists(string Filename, byte[] Bytedata) 
     { 

      string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

      var filename = Path.Combine(dirPath, Filename); 
      FileInfo fi = new FileInfo(filename); 

      if (!NSFileManager.DefaultManager.FileExists(filename)) 
      { 
       Stream stream = new MemoryStream(Bytedata); 
       NSData imgData = NSData.FromStream(stream); 
       NSError err; 
       imgData.Save(filename, false, out err); 
       return filename; 

      } 
      else 
      { 
       return filename; 
      } 
     } 
    } 

    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 IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) 
     { 
      return (IQLPreviewItem)new PDFItem(filename, url); 
     } 

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

用法:

IDataViewer dataViewer = DependencyService.Get<IDataViewer>(); 
dataViewer.showPhoto(FileName, AttachmentBytes); 
相关问题