1.概要
最近在遷移 GeneralUpdate.Tool的時候需要用到文件夾選擇,在MAUI中可以使用FolderPicker進行選擇。注意,和上篇文章的文件選擇不一樣。因為在.NET MAUI中目前還沒有傻瓜式直接可用的FolderPicker供開發者使用所以需要自己動手做一些修改。
完整示例代碼:https://gitee.com/Juster-zhu/GeneralUpdate/tree/master/src/c%23/GeneralUpdate.PacketTool
2.詳細內容
實現步驟如下:
定義接口
public interface IFolderPickerService{Task<string> PickFolderTaskAsync();}
2.在每個受支持的平臺上實現接口
using GeneralUpdate.Infrastructure.DataServices.Pick;
using WindowsFolderPicker = Windows.Storage.Pickers.FolderPicker;namespace GeneralUpdate.PacketTool.Platforms.Windows
{public class FolderPicker : IFolderPickerService{public async Task<string> PickFolderTaskAsync(){var folderPicker = new WindowsFolderPicker();// Might be needed to make it work on Windows 10folderPicker.FileTypeFilter.Add("*");// Get the current window's HWND by passing in the Window objectvar hwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;// Associate the HWND with the file pickerWinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hwnd);var result = await folderPicker.PickSingleFolderAsync();return result?.Path;}}
}
3.向.NET MAUI框架容器中注入FolderPicker注冊實現
一定需要記住下面代碼中的這個using引用。
using GeneralUpdate.Infrastructure.DataServices.Pick;
不可以刪除因為加入了環境的判斷會導致在編碼時認為是無效應用,實際運行時會使用到該命名空間。
#if WINDOWSmauiAppBuilder.Services.AddTransient<IFolderPickerService, Platforms.Windows.FolderPicker>();
#elif MACCATALYSTmauiAppBuilder.Services.AddTransient<IFolderPickerService, Platforms.MacCatalyst.FolderPicker>();
#endif
實際代碼如下。
using GeneralUpdate.Infrastructure.DataServices.Pick;
using GeneralUpdate.PacketTool.ViewModels;namespace GeneralUpdate.PacketTool
{public static class MauiProgram{public static MauiApp CreateMauiApp(){var builder = MauiApp.CreateBuilder();builder.UseMauiApp<App>().RegisterViewModels().RegisterView().RegisterAppServices().RegisterOther().ConfigureFonts(fonts =>{fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");});return builder.Build();}public static MauiAppBuilder RegisterOther(this MauiAppBuilder mauiAppBuilder){mauiAppBuilder.Services.AddTransient<App>();return mauiAppBuilder;}public static MauiAppBuilder RegisterView(this MauiAppBuilder mauiAppBuilder){mauiAppBuilder.Services.AddTransient<MainPage>();return mauiAppBuilder;}public static MauiAppBuilder RegisterViewModels(this MauiAppBuilder mauiAppBuilder){mauiAppBuilder.Services.AddTransient<MainViewModel>();return mauiAppBuilder;}public static MauiAppBuilder RegisterAppServices(this MauiAppBuilder mauiAppBuilder){
#if WINDOWSmauiAppBuilder.Services.AddTransient<IFolderPickerService, Platforms.Windows.FolderPicker>();
#elif MACCATALYSTmauiAppBuilder.Services.AddTransient<IFolderPickerService, Platforms.MacCatalyst.FolderPicker>();
#endifreturn mauiAppBuilder;}}
}
4.使用功能
如何使用
將我們剛剛在容器中注入好的FolderPickerService取出來,并初始化ViewModel中的引用。
public class MainViewModel : ViewModeBase{//code...public MainViewModel(IFolderPickerService folderPickerService) {_folderPickerService = folderPickerService;}//code...}
FolderPickerService調用。
/// <summary>/// Choose a path/// </summary>/// <param name="value"></param>private async Task SelectFolderAction(string value){var pickerResult = await _folderPickerService.PickFolderTaskAsync();if (pickerResult == null){await Shell.Current.DisplayAlert("Pick options", "No results were selected !", "ok");return;}switch (value){case "Source":SourcePath = pickerResult;break;case "Target":TargetPath = pickerResult;break;case "Patch":PatchPath = pickerResult;break;}}
運行效果如下