我正在开发Xamarin Forms应用程序。该应用程序当前从URL下载文件并将其保存在手机的Documents目录中。 我现在打算实现的是在Button Click上查看下载的文件。该应用程序弹出可用于打开文件的默认程序。
我在android中已经这样做了。但是我在iOS上遇到了问题。 我正在使用" UIDocumentInteractionController"查看文件。但按钮点击事件不会显示任何选项。
此代码有问题吗?
NSUrl URL = new NSUrl(filePath, false);
var viewer = UIDocumentInteractionController.FromUrl(URL);
viewer.PresentOpenInMenu(new RectangleF(0, -260, 320, 320), this.View, true);
以下是我的完整代码示例。请帮忙。
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using Foundation;
using Plugin.DownloadManager;
using Plugin.DownloadManager.Abstractions;
using UIKit;
namespace PracticeApp.iOS
{
public partial class ViewController : UIViewController
{
string filePath;
string fileName;
public ViewController(IntPtr handle) : base(handle)
{
// SOME CODE
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// SOME CODE FOR DOWNLOADIND THE FILE FROM URL
// Downloaded File Path
// /Users/user/Library/Developer/CoreSimulator/Devices/6A3459AC-A7BC-40CE-A3F9-6581215E3421/data/Containers/Data/Application/BC5B26E9-ABD9-4310-96F0-57194837A73E/Documents/test1.pptx
// AFTER DOWNLOAD FINISHED
var buttonO = new UIButton(UIButtonType.RoundedRect) {
Frame = UIScreen.MainScreen.Bounds,
BackgroundColor = UIColor.Red
};
NSString urlString = new NSString(filePath);
NSUrl myFileUrl = new NSUrl(urlString);
Console.WriteLine(myFileUrl.AbsoluteString);
string absPath = myFileUrl.Path;
buttonO.TouchUpInside += (sender1, e1) => {
System.Diagnostics.Debug.WriteLine("File Path: " + filePath);
OpenFileFunc();
};
Add(buttonO);
}
public void OpenFileFunc()
{
var firstController = UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers.First().ChildViewControllers.Last().ChildViewControllers.First();
var navcontroller = firstController as UINavigationController;
//var UIDocumentInteractionController = new UIDocumentInteractionController();
//UIDocumentInteractionController.Url = filePath;
//UIDocumentInteractionController.Delegate = new DocInteractionC(navcontroller);
//UIDocumentInteractionController.PresentPreview(true);
var controller = GetVisibleViewController();
var viewer = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);
}
//public class DocInteractionC : UIDocumentInteractionControllerDelegate
//{
// readonly UINavigationController _navigationController;
// public DocInteractionC(UINavigationController controller)
// {
// _navigationController = controller;
// }
// public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
// {
// return _navigationController;
// }
// public override UIView ViewForPreview(UIDocumentInteractionController controller)
// {
// return _navigationController.View;
// }
//}
private UIViewController GetVisibleViewController(UIViewController controller = null)
{
controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;
if (controller.PresentedViewController == null)
return controller;
if (controller.PresentedViewController is UINavigationController) {
return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
}
if (controller.PresentedViewController is UITabBarController) {
return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
}
return GetVisibleViewController(controller.PresentedViewController);
}
}
}