Xamarin.Formsアプリケーションでは、 UIDocumentPickerViewControllerを使用してユーザーにファイル ピッカーを表示しています。iOS 13.0 以降で使用できる1 つのプロパティDirectoryUrlがあります。基本的にドキュメントに従って、このプロパティはファイル ピッカーの初期ディレクトリを設定します。
ドキュメント リンク: https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller/3183918-directoryurl
DirectoryUrlプロパティを設定すると、最初に設定されたディレクトリでファイル ピッカーが開き、ファイル ピッカーで参照タブが選択されますが、上部にキャンセル ボタンが表示されません。
ユーザーが最近のタブに移動し、ブラウズ タブに戻ると、キャンセル ボタンのみが表示されます。
ユーザーが [最近] タブの [キャンセル] ボタンを使用してファイル ピッカーをキャンセルすると、[参照] タブの [キャンセル] ボタンも表示されなくなります。
コード:
var allowedUtis = new string[]
{
UTType.Content,
UTType.Item,
UTType.Data
};
if (allowedTypes != null)
{
allowedUtis = allowedTypes;
}
var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Import);
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
// DirectoryUrl property only works in iOS SDK 13.0+
// _documentsPath is path for folder
docPicker.DirectoryUrl = new NSUrl(_documentsPath, true);
}
documentPicker.DidPickDocument += this.DocumentPicker_DidPickDocument;
documentPicker.WasCancelled += this.DocumentPicker_WasCancelled;
documentPicker.DidPickDocumentAtUrls += this.DocumentPicker_DidPickDocumentAtUrls;
UIViewController viewController = GetActiveViewController();
viewController.PresentViewController(documentPicker, true, null);