Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
<PackageReference Include="Microsoft.Maui.Core" Version="$(MauiPackageVersion)" />
<PackageReference Include="Microsoft.Maui.Essentials" Version="$(MauiPackageVersion)" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" Condition=" '$(Configuration)'=='Release' " PrivateAssets="All" />
<PackageReference Include="System.Speech" Version="10.0.0" Condition="'$(TargetFramework)' == '$(NetVersion)-windows10.0.19041.0'" />
</ItemGroup>

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

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

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using Windows.Storage.Pickers;
using Microsoft.Windows.Storage.Pickers;

namespace CommunityToolkit.Maui.Storage;

Expand All @@ -10,11 +11,19 @@ public sealed partial class FileSaverImplementation : IFileSaver

async Task<string> InternalSaveAsync(string initialPath, string fileName, Stream stream, IProgress<double>? progress, CancellationToken cancellationToken)
{
var savePicker = new FileSavePicker
var window = IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView as MauiWinUIWindow;
if (window is null)
{
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))
Expand All @@ -23,15 +32,18 @@ async Task<string> 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);
await using var taskCompetedSource = 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
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;

Expand All @@ -10,12 +14,18 @@ public sealed partial class FolderPickerImplementation : IFolderPicker
async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var folderPicker = new Windows.Storage.Pickers.FolderPicker()
var window = IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView as MauiWinUIWindow;
if (window is null)
{
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()
Expand All @@ -27,10 +37,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<Folder> InternalPickAsync(CancellationToken cancellationToken)
Expand Down
Loading