简体   繁体   English

文件夹选择器 .NET MAUI

[英]Folder Picker .NET MAUI

I would to do a downloader app that save pictures to a folder.我想做一个将图片保存到文件夹的下载器应用程序。 The app should work on windows and macos, and may be later on android and ios.该应用程序应该在 windows 和 macos 上运行,并且可能稍后在 android 和 ios 上运行。

I haven't found a way to pick the target folder.我还没有找到选择目标文件夹的方法。 Any idea on how it can be achieve either with blazor or xaml .NET MAUI app?关于如何使用 blazor 或 xaml .NET MAUI 应用程序实现它的任何想法?

I've made a start implementing this for Windows and macOS.我已经开始为 Windows 和 macOS 实现这个。 You can review the code here: https://github.com/jfversluis/MauiFolderPickerSample and wrote a little blog post about this as well here: https://blog.verslu.is/maui/folder-picker-with-dotnet-maui/您可以在此处查看代码: https://github.com/jfversluis/MauiFolderPickerSample并在此处也写了一篇关于此的小博客文章: https://blog.verslu.is/maui/folder-picker-with-dotnet-毛伊岛/

This follows kind of the basic pattern you'd want to use if you want to access platform-specific APIs:如果您想访问特定于平台的 API,这将遵循您想要使用的基本模式:

  • Define an interface定义一个接口
  • Implement interface on each supported platform在每个支持的平台上实现接口
  • Consume functionality消费功能

For this I have created a very simple but effective interface为此,我创建了一个非常简单但有效的界面

public interface IFolderPicker
{
    Task<string> PickFolder();
}

Then we create an implementation for Windows, by adding a new file FilePicker.cs to the Platforms\Windows\ folder.然后,我们通过将新文件FilePicker.cs添加到Platforms\Windows\文件夹来为 Windows 创建一个实现。 This makes it specific to Windows and allows us to write Windows specific code.这使它特定于 Windows 并允许我们编写 Windows 特定代码。 The file contains this code:该文件包含以下代码:

using WindowsFolderPicker = Windows.Storage.Pickers.FolderPicker;

namespace MauiFolderPickerSample.Platforms.Windows
{
    public class FolderPicker : IFolderPicker
    {
        public async Task<string> PickFolder()
        {
            var folderPicker = new WindowsFolderPicker();
            // Make it work for Windows 10
            folderPicker.FileTypeFilter.Add("*");
            // Get the current window's HWND by passing in the Window object
            var hwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;

            // Associate the HWND with the file picker
            WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hwnd);

            var result = await folderPicker.PickSingleFolderAsync();

            return result.Path;
        }
    }
}

Because I chose FolderPicker as the name for my own object here, there is a naming conflict with the Windows FolderPicker that is why there is that weird using at the top.因为我在这里选择了FolderPicker作为我自己的 object 的名称,所以与 Windows FolderPicker存在命名冲突,这就是为什么在顶部使用奇怪的原因。 If you go for MyFolderPicker as your object name that wouldn't be needed.如果您将MyFolderPicker的 go 作为您的 object 名称,则不需要。

Now we register this interface and implementation with the generic host builder in our MauiProgram.cs :现在我们在MauiProgram.cs中使用通用主机构建器注册这个接口和实现:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

// Note: this part was added

#if WINDOWS
        builder.Services.AddTransient<IFolderPicker, Platforms.Windows.FolderPicker>();
#elif MACCATALYST
        builder.Services.AddTransient<IFolderPicker, Platforms.MacCatalyst.FolderPicker>();
#endif
        builder.Services.AddTransient<MainPage>();
        builder.Services.AddTransient<App>();
// Note: end added part

        return builder.Build();
    }
}

Note that I also added MainPage and App here so that our constructor injection works (have a look at MainPage.xaml.cs in the linked repository).请注意,我还在此处添加了MainPageApp ,以便我们的构造函数注入工作(查看链接存储库中的MainPage.xaml.cs )。

Now we can consume our functionality as follows:现在我们可以按如下方式使用我们的功能:

namespace MauiFolderPickerSample;

public partial class MainPage : ContentPage
{
    private readonly IFolderPicker _folderPicker;

    public MainPage(IFolderPicker folderPicker)
    {
        InitializeComponent();
        _folderPicker = folderPicker;
    }

    private async void OnPickFolderClicked(object sender, EventArgs e)
    {
        var pickedFolder = await _folderPicker.PickFolder();

        FolderLabel.Text = pickedFolder;

        SemanticScreenReader.Announce(FolderLabel.Text);
    }
}

Implementing other platforms would require you to implement the interface for the platform you want to support and register it in the generic host builder.实现其他平台需要您为要支持的平台实现接口并将其注册到通用主机构建器中。 This should get you started for Windows and macOS.这应该让您开始使用 Windows 和 macOS。

Actually calling this should not be any different between .NET MAUI (regular) or .NET MAUI Blazor.实际上,在 .NET MAUI(常规)或 .NET MAUI Blazor 之间调用这个应该没有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM