Skip to content

Commit bdd2007

Browse files
Merge branch 'main' into Remove-CommunityToolkit.Maui.SourceGenerators.Internals.-
2 parents 9ec8019 + d12df7f commit bdd2007

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FileSaverViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ async Task SaveFile(CancellationToken cancellationToken)
3838
try
3939
{
4040
var fileName = Application.Current?.Windows[0].Page?.DisplayPromptAsync("FileSaver", "Choose filename") ?? Task.FromResult("test.txt");
41-
var fileLocationResult = await fileSaver.SaveAsync(await fileName, stream, cancellationToken);
41+
var initialFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
42+
var fileLocationResult = await fileSaver.SaveAsync(initialFolder, await fileName, stream, cancellationToken);
4243
fileLocationResult.EnsureSuccess();
4344

4445
await Toast.Make($"File is saved: {fileLocationResult.FilePath}").Show(cancellationToken);

samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/FolderPickerViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ async Task PickFolder(CancellationToken cancellationToken)
3333
return;
3434
}
3535

36-
var folderPickerResult = await folderPicker.PickAsync(cancellationToken);
36+
var initialFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
37+
var folderPickerResult = await folderPicker.PickAsync(initialFolder, cancellationToken);
3738
if (folderPickerResult.IsSuccessful)
3839
{
3940
await Toast.Make($"Folder picked: Name - {folderPickerResult.Folder.Name}, Path - {folderPickerResult.Folder.Path}", ToastDuration.Long).Show(cancellationToken);

src/CommunityToolkit.Maui.Core/CommunityToolkit.Maui.Core.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
<PackageReference Include="Microsoft.Maui.Core" Version="$(MauiPackageVersion)" />
5656
<PackageReference Include="Microsoft.Maui.Essentials" Version="$(MauiPackageVersion)" />
5757
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" Condition=" '$(Configuration)'=='Release' " PrivateAssets="All" />
58-
<PackageReference Include="System.Speech" Version="10.0.0" Condition="'$(TargetFramework)' == '$(NetVersion)-windows10.0.19041.0'" />
5958
</ItemGroup>
6059

6160
<ItemGroup Condition="$(TargetFramework.Contains('-tizen'))">
@@ -64,4 +63,9 @@
6463
<PackageReference Include="Tizen.UIExtensions.NUI" Version="*" />
6564
</ItemGroup>
6665

66+
<ItemGroup Condition="'$(TargetFramework)' == '$(NetVersion)-windows10.0.19041.0'">
67+
<PackageReference Include="System.Speech" Version="10.0.1" />
68+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.0-experimental3" />
69+
</ItemGroup>
70+
6771
</Project>

src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.windows.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Diagnostics;
2-
using Windows.Storage.Pickers;
1+
using Microsoft.Windows.Storage.Pickers;
32

43
namespace CommunityToolkit.Maui.Storage;
54

@@ -10,11 +9,18 @@ public sealed partial class FileSaverImplementation : IFileSaver
109

1110
async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream stream, IProgress<double>? progress, CancellationToken cancellationToken)
1211
{
13-
var savePicker = new FileSavePicker
12+
if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
1413
{
15-
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
14+
throw new FileSaveException(
15+
"Cannot present file picker: No active window found. Ensure the app is active with a visible window.");
16+
}
17+
18+
var savePicker = new FileSavePicker(window.AppWindow.Id)
19+
{
20+
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
21+
SuggestedFolder = initialPath,
22+
SuggestedFileName = Path.GetFileNameWithoutExtension(fileName)
1623
};
17-
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, Process.GetCurrentProcess().MainWindowHandle);
1824

1925
var extension = Path.GetExtension(fileName);
2026
if (!string.IsNullOrEmpty(extension))
@@ -23,15 +29,18 @@ async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream
2329
}
2430

2531
savePicker.FileTypeChoices.Add("All files", allFilesExtension);
26-
savePicker.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName);
2732

2833
var filePickerOperation = savePicker.PickSaveFileAsync();
29-
3034
await using var _ = cancellationToken.Register(CancelFilePickerOperation);
3135
var file = await filePickerOperation;
32-
if (string.IsNullOrEmpty(file?.Path))
36+
if (file is null)
37+
{
38+
throw new OperationCanceledException("Operation cancelled.");
39+
}
40+
41+
if (string.IsNullOrEmpty(file.Path))
3342
{
34-
throw new FileSaveException("Operation cancelled or Path doesn't exist.");
43+
throw new FileSaveException("Path doesn't exist.");
3544
}
3645

3746
await WriteStream(stream, file.Path, progress, cancellationToken).ConfigureAwait(false);

src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.windows.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Diagnostics;
21
using CommunityToolkit.Maui.Core.Primitives;
3-
using Windows.Storage.Pickers;
2+
using Microsoft.Maui.ApplicationModel;
3+
using Microsoft.UI;
4+
using Microsoft.Windows.AppLifecycle;
5+
using Microsoft.Windows.Storage.Pickers;
46

57
namespace CommunityToolkit.Maui.Storage;
68

@@ -10,12 +12,18 @@ public sealed partial class FolderPickerImplementation : IFolderPicker
1012
async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cancellationToken)
1113
{
1214
cancellationToken.ThrowIfCancellationRequested();
13-
var folderPicker = new Windows.Storage.Pickers.FolderPicker()
15+
if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
1416
{
15-
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
17+
throw new FolderPickerException(
18+
"Cannot present folder picker: No active window found. Ensure the app is active with a visible window.");
19+
}
20+
21+
var folderPicker = new Microsoft.Windows.Storage.Pickers.FolderPicker(window.AppWindow.Id)
22+
{
23+
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
24+
SuggestedFolder = initialPath
1625
};
17-
WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, Process.GetCurrentProcess().MainWindowHandle);
18-
folderPicker.FileTypeFilter.Add("*");
26+
1927
var folderPickerOperation = folderPicker.PickSingleFolderAsync();
2028

2129
void CancelFolderPickerOperation()
@@ -27,10 +35,15 @@ void CancelFolderPickerOperation()
2735
var folder = await folderPickerOperation;
2836
if (folder is null)
2937
{
30-
throw new FolderPickerException("Operation cancelled or Folder doesn't exist.");
38+
throw new OperationCanceledException("Operation cancelled.");
39+
}
40+
41+
if (string.IsNullOrEmpty(folder.Path))
42+
{
43+
throw new FolderPickerException("Folder doesn't exist.");
3144
}
3245

33-
return new Folder(folder.Path, folder.Name);
46+
return new Folder(folder.Path, new DirectoryInfo(folder.Path).Name);
3447
}
3548

3649
Task<Folder> InternalPickAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)