Skip to content

Commit 79082e5

Browse files
Refactor picker window handling and csproj conditions
Refactored window retrieval in file/folder pickers to use pattern matching for clarity and modern C# style. Updated picker instantiation to use AppWindow.Id directly. Removed unused using statements. Changed csproj Windows ItemGroup condition to use $(NetVersion) for better .NET version flexibility. Cleaned up cancellation token registration variable naming.
1 parent 38a298c commit 79082e5

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<PackageReference Include="Tizen.UIExtensions.NUI" Version="*" />
6464
</ItemGroup>
6565

66-
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
66+
<ItemGroup Condition="'$(TargetFramework)' == '$(NetVersion)-windows10.0.19041.0'">
6767
<PackageReference Include="System.Speech" Version="10.0.1" />
6868
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.0-experimental3" />
6969
</ItemGroup>

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Diagnostics;
31
using Microsoft.Windows.Storage.Pickers;
42

53
namespace CommunityToolkit.Maui.Storage;
@@ -11,13 +9,12 @@ public sealed partial class FileSaverImplementation : IFileSaver
119

1210
async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream stream, IProgress<double>? progress, CancellationToken cancellationToken)
1311
{
14-
var window = IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView as MauiWinUIWindow;
15-
if (window is null)
12+
if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
1613
{
1714
throw new FileSaveException(
1815
"Cannot present file picker: No active window found. Ensure the app is active with a visible window.");
1916
}
20-
17+
2118
var savePicker = new FileSavePicker(window.AppWindow.Id)
2219
{
2320
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
@@ -34,7 +31,7 @@ async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream
3431
savePicker.FileTypeChoices.Add("All files", allFilesExtension);
3532

3633
var filePickerOperation = savePicker.PickSaveFileAsync();
37-
await using var taskCompetedSource = cancellationToken.Register(CancelFilePickerOperation);
34+
await using var _ = cancellationToken.Register(CancelFilePickerOperation);
3835
var file = await filePickerOperation;
3936
if (file is null)
4037
{

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Diagnostics;
31
using CommunityToolkit.Maui.Core.Primitives;
42
using Microsoft.Maui.ApplicationModel;
53
using Microsoft.UI;
@@ -14,12 +12,12 @@ public sealed partial class FolderPickerImplementation : IFolderPicker
1412
async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cancellationToken)
1513
{
1614
cancellationToken.ThrowIfCancellationRequested();
17-
var window = IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView as MauiWinUIWindow;
18-
if (window is null)
15+
if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
1916
{
2017
throw new FolderPickerException(
2118
"Cannot present folder picker: No active window found. Ensure the app is active with a visible window.");
2219
}
20+
2321
var folderPicker = new Microsoft.Windows.Storage.Pickers.FolderPicker(window.AppWindow.Id)
2422
{
2523
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,

0 commit comments

Comments
 (0)