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

How to write a method to open an image in C# that will use the default image viewer on a mobile device...?

抱歉标题太长了!

我对大学里学的 C# 还很陌生(可能只有 2-3 个月的具体知识)...

我一直在试验 Xamarin Forms XAML,我想知道如何在 code-behind 中编写一个方法来打开要在默认图像查看器中打开的点击图像 [= =22=] 或 iOS.

我说 Android 或 iOS 因为我正在做 cross-platform Xamarin Forms PCL.

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

尝试以下代码:

PCL 界面:

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

特定平台(Droid):

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);