diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FileSaverViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FileSaverViewModel.cs
index f117344c17..6536ea3190 100644
--- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FileSaverViewModel.cs
+++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FileSaverViewModel.cs
@@ -38,7 +38,8 @@ async Task SaveFile(CancellationToken cancellationToken)
try
{
var fileName = Application.Current?.Windows[0].Page?.DisplayPromptAsync("FileSaver", "Choose filename") ?? Task.FromResult("test.txt");
- var fileLocationResult = await fileSaver.SaveAsync(await fileName, stream, cancellationToken);
+ var initialFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
+ var fileLocationResult = await fileSaver.SaveAsync(initialFolder, await fileName, stream, cancellationToken);
fileLocationResult.EnsureSuccess();
await Toast.Make($"File is saved: {fileLocationResult.FilePath}").Show(cancellationToken);
diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FolderPickerViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FolderPickerViewModel.cs
index a0525903f5..d5f5c4aa46 100644
--- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FolderPickerViewModel.cs
+++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FolderPickerViewModel.cs
@@ -33,7 +33,8 @@ async Task PickFolder(CancellationToken cancellationToken)
return;
}
- var folderPickerResult = await folderPicker.PickAsync(cancellationToken);
+ var initialFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
+ var folderPickerResult = await folderPicker.PickAsync(initialFolder, cancellationToken);
if (folderPickerResult.IsSuccessful)
{
await Toast.Make($"Folder picked: Name - {folderPickerResult.Folder.Name}, Path - {folderPickerResult.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
diff --git a/src/CommunityToolkit.Maui.Core/CommunityToolkit.Maui.Core.csproj b/src/CommunityToolkit.Maui.Core/CommunityToolkit.Maui.Core.csproj
index 215f93c224..a232f52bf9 100644
--- a/src/CommunityToolkit.Maui.Core/CommunityToolkit.Maui.Core.csproj
+++ b/src/CommunityToolkit.Maui.Core/CommunityToolkit.Maui.Core.csproj
@@ -55,7 +55,6 @@
-
@@ -64,4 +63,9 @@
+
+
+
+
+
diff --git a/src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.windows.cs b/src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.windows.cs
index c385019776..615167933c 100644
--- a/src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.windows.cs
+++ b/src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.windows.cs
@@ -1,5 +1,4 @@
-using System.Diagnostics;
-using Windows.Storage.Pickers;
+using Microsoft.Windows.Storage.Pickers;
namespace CommunityToolkit.Maui.Storage;
@@ -10,11 +9,18 @@ public sealed partial class FileSaverImplementation : IFileSaver
async Task InternalSaveAsync(string initialPath, string fileName, Stream stream, IProgress? progress, CancellationToken cancellationToken)
{
- var savePicker = new FileSavePicker
+ if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
{
- SuggestedStartLocation = PickerLocationId.DocumentsLibrary
+ throw new FileSaveException(
+ "Cannot present file picker: No active window found. Ensure the app is active with a visible window.");
+ }
+
+ var savePicker = new FileSavePicker(window.AppWindow.Id)
+ {
+ SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
+ SuggestedFolder = initialPath,
+ SuggestedFileName = Path.GetFileNameWithoutExtension(fileName)
};
- WinRT.Interop.InitializeWithWindow.Initialize(savePicker, Process.GetCurrentProcess().MainWindowHandle);
var extension = Path.GetExtension(fileName);
if (!string.IsNullOrEmpty(extension))
@@ -23,15 +29,18 @@ async Task InternalSaveAsync(string initialPath, string fileName, Stream
}
savePicker.FileTypeChoices.Add("All files", allFilesExtension);
- savePicker.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName);
var filePickerOperation = savePicker.PickSaveFileAsync();
-
await using var _ = cancellationToken.Register(CancelFilePickerOperation);
var file = await filePickerOperation;
- if (string.IsNullOrEmpty(file?.Path))
+ if (file is null)
+ {
+ throw new OperationCanceledException("Operation cancelled.");
+ }
+
+ if (string.IsNullOrEmpty(file.Path))
{
- throw new FileSaveException("Operation cancelled or Path doesn't exist.");
+ throw new FileSaveException("Path doesn't exist.");
}
await WriteStream(stream, file.Path, progress, cancellationToken).ConfigureAwait(false);
diff --git a/src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.windows.cs b/src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.windows.cs
index 4a557887d9..2b3965fce7 100644
--- a/src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.windows.cs
+++ b/src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.windows.cs
@@ -1,6 +1,8 @@
-using System.Diagnostics;
using CommunityToolkit.Maui.Core.Primitives;
-using Windows.Storage.Pickers;
+using Microsoft.Maui.ApplicationModel;
+using Microsoft.UI;
+using Microsoft.Windows.AppLifecycle;
+using Microsoft.Windows.Storage.Pickers;
namespace CommunityToolkit.Maui.Storage;
@@ -10,12 +12,18 @@ public sealed partial class FolderPickerImplementation : IFolderPicker
async Task InternalPickAsync(string initialPath, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
- var folderPicker = new Windows.Storage.Pickers.FolderPicker()
+ if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
{
- SuggestedStartLocation = PickerLocationId.DocumentsLibrary
+ throw new FolderPickerException(
+ "Cannot present folder picker: No active window found. Ensure the app is active with a visible window.");
+ }
+
+ var folderPicker = new Microsoft.Windows.Storage.Pickers.FolderPicker(window.AppWindow.Id)
+ {
+ SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
+ SuggestedFolder = initialPath
};
- WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, Process.GetCurrentProcess().MainWindowHandle);
- folderPicker.FileTypeFilter.Add("*");
+
var folderPickerOperation = folderPicker.PickSingleFolderAsync();
void CancelFolderPickerOperation()
@@ -27,10 +35,15 @@ void CancelFolderPickerOperation()
var folder = await folderPickerOperation;
if (folder is null)
{
- throw new FolderPickerException("Operation cancelled or Folder doesn't exist.");
+ throw new OperationCanceledException("Operation cancelled.");
+ }
+
+ if (string.IsNullOrEmpty(folder.Path))
+ {
+ throw new FolderPickerException("Folder doesn't exist.");
}
- return new Folder(folder.Path, folder.Name);
+ return new Folder(folder.Path, new DirectoryInfo(folder.Path).Name);
}
Task InternalPickAsync(CancellationToken cancellationToken)