Xamarin Forms-如何打开存储在本地存储中的文件

Xamarin Forms-如何打开存储在本地存储中的文件,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我们需要使用Xamarin.Forms从移动应用程序的本地存储中打开MS office&media文件(.docx、.xlsx、pptx、.png、.bmp、.mp4、.avi等) 我们不需要打开应用程序中的文件。只是,我们需要在设备中调用相应的应用程序来打开其文件类型 我们已尝试: Device.OpenUri(新的Uri(“https://www.w3.org/TR/PNG/iso_8859-1.txt“”)但是文本文件正在浏览器中打开,我们希望在其应用程序中打开这些文件,或者至少会出现一个

我们需要使用Xamarin.Forms从移动应用程序的本地存储中打开MS office&media文件(.docx、.xlsx、pptx、.png、.bmp、.mp4、.avi等)

我们不需要打开应用程序中的文件。只是,我们需要在设备中调用相应的应用程序来打开其文件类型

我们已尝试:
Device.OpenUri(新的Uri(“https://www.w3.org/TR/PNG/iso_8859-1.txt“”)
但是文本文件正在浏览器中打开,我们希望在其应用程序中打开这些文件,或者至少会出现一个窗口,要求选择要打开文件的应用程序


注意:我们使用的是Xamarin.Forms for Windows、Android和iOS应用程序。

请查看此链接是否为您提供了答案:

您必须为此使用DependecyService。以下是我的解决方案:


我已经从java绑定了一个库,但我还没有在IOS上测试它,所以它无法运行

当我在安卓上使用它的时候,安静很好 这是演示:

您将无法在所有平台上获得相同的体验。对于Android,请看意图。对于iOS(我认为Windows也是如此)来说,请查看自定义URL方案。但尤其是苹果公司在检测设备上有哪些其他应用程序方面实施了严格的政策。所以这需要一些努力。我要强调,;您不可能以共享代码的方式执行此操作
//Interface in PCL
public interface IDocumentViewer
{
void ShowDocumentFile(string filepaht, string mimeType);
}

//Android
[assembly: Dependency(typeof(DocumentViewer_Droid))]

namespace YourApp.Droid
{
public class DocumentViewer_Droid : IDocumentViewer
{
    public void ShowDocumentFile(string filepath, string mimeType
    {
        var uri = Android.Net.Uri.Parse("file://" + filepath);
        var intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, mimeType);
        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

        try
        {
            Forms.Context.StartActivity(Intent.CreateChooser(intent, "Select App"));
        }
        catch(Exception ex)
        {
            //Let the user know when something went wrong
        }
    }
}
}

//iOS
[assembly: Dependency(typeof(DocumentViewer_iOS))]

namespace.YourApp.iOS
{
public class DocumentViewer_iOS : IDocumentViewer
{
    public void ShowDocumentFile(string filepath, string mimeType)
    {
        var fileinfo = new FileInfo(filepath);
        var previewController = new QLPreviewController();
        previewController.DataSource = new PreviewControllerDataSource(fileinfo.FullName, fileinfo.Name);

        UINavigationController controller = FindNavigationController();

        if(controller != null)
        {
            controller.PresentViewController((UIViewController)previewController, true, (Action)null);
        }
    }

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

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

            return null;
        }
    }
}

public class DocumentItem : QLPreviewItem
{
    private string _title;
    private string _uri;

    public DocumentItem(string title, string uri)
    {
        _title = title;
        _uri = uri;
    }

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

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

public class PreviewControllerDataSource : QLPreviewControllerDataSource
{
    private string _url;
    private string _filename;

    public PreviewControllerDataSource(string url, string filename)
    {
        _url = url;
        _filename = filename;
    }

    public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
    {
        return (IQLPreviewItem)new DocumentItem(_filename, _url);
    }

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