Fre*_*Ali 6 xamarin.ios xamarin xamarin.forms
我正在使用Xamarin表单并为以下目标编写依赖服务:
打开iOS文件应用.(UIDocumentPickerViewController)
选择任何类型的文档.
将该文档复制到我的应用程序文档目录.(适用于应用访问)
通过将其路径存储到我的SQLite DB中,将该文档显示到我的应用程序中.
我在这里尝试做的是在条目点击上从我的应用程序调用Files应用程序并且click事件似乎运行良好我的依赖服务调用完美但现在当我尝试使用UIDocumentPickerViewController时我无法获取View控制器上下文在我的依赖服务中调用PresentViewController方法.现在我知道了xamarin表单的上下文,但我不知道它是否会在这里工作,我甚至不知道使用它是否是一个明智的想法,因为它已被标记为已过时,因为我不是来自iOS背景,我不知道什么是正确的解决方案.
我的代码如下:
public class DocumentPickerRenderer : IDocumentPicker
{
public object PickFile()
{
var docPicker = new UIDocumentPickerViewController(new string[] { UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
docPicker.WasCancelled += (sender, wasCancelledArgs) =>
{
};
docPicker.DidPickDocumentAtUrls += (object sender, UIDocumentPickedAtUrlsEventArgs e) =>
{
Console.WriteLine("url = {0}", e.Urls[0].AbsoluteString);
//bool success = await MoveFileToApp(didPickDocArgs.Url);
var success = true;
string filename = e.Urls[0].LastPathComponent;
string msg = success ? string.Format("Successfully imported file '{0}'", filename) : string.Format("Failed to import file '{0}'", filename);
var alertController = UIAlertController.Create("import", msg, UIAlertControllerStyle.Alert);
var okButton = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) =>
{
alertController.DismissViewController(true, null);
});
alertController.AddAction(okButton);
PresentViewController(alertController, true, null);
};
PresentViewController(docPicker, true, null);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
我的方法是否适合挑选文件?
作为文件选择的回调,我将获得什么样的对象以及如何获得回调?
是否有任何其他方式或东西可用于xamarin表单,一些指南或允许我从我的本机文件系统中选择文档的东西,并简要介绍如何在ios和android中处理它?
因为我正在寻找UIDocumentPickerViewController
而不是UIDocumentMenuViewController
其他答案不是我正在寻找的:
这就是我最终这样做的方式:
调用文档选择器:
var docPicker = new UIDocumentPickerViewController(new string[]
{ UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
docPicker.WasCancelled += DocPicker_WasCancelled;
docPicker.DidPickDocumentAtUrls += DocPicker_DidPickDocumentAtUrls;
docPicker.DidPickDocument += DocPicker_DidPickDocument;
var _currentViewController = GetCurrentUIController();
if (_currentViewController != null)
_currentViewController.PresentViewController(docPicker, true, null);
Run Code Online (Sandbox Code Playgroud)
其中 GetCurrentUIController 是获取当前 UI 控制器的函数,如下所示:
public UIViewController GetCurrentUIController()
{
UIViewController viewController;
var window = UIApplication.SharedApplication.KeyWindow;
if (window == null)
{
return null;
}
if (window.RootViewController.PresentedViewController == null)
{
window = UIApplication.SharedApplication.Windows
.First(i => i.RootViewController != null &&
i.RootViewController.GetType().FullName
.Contains(typeof(Xamarin.Forms.Platform.iOS.Platform).FullName));
}
viewController = window.RootViewController;
while (viewController.PresentedViewController != null)
{
viewController = viewController.PresentedViewController;
}
return viewController;
}
Run Code Online (Sandbox Code Playgroud)
对于以下 iOS 11,我添加了该DidPickDocument
事件:
private void DocPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
try
{
NSUrl filePath = e.Url.AbsoluteUrl;
//This is the url for your document and you can use it as you please.
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
对于上述 iOS 11,您可以使用多重选择DidPickDocumentUrls
,因为那里支持多重选择:
private void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
{
try
{
List<NSUrl> filePath = e.Urls.ToList().Select(y => y.AbsoluteUrl).ToList();
//returns the list of images selected
}
catch (Exception ex)
{
AppLogger.LogException(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
900 次 |
最近记录: |