Executing FileSavePicker.PickSaveFileAsync on .NET MAUI causes the application to crash.
Executing following code on .NET MAUI causes the application to crash.
public async Task<bool> ShowAsync(string fileName, byte[] bytes, SaveFilePickerOptions options)
{
ArgumentNullException.ThrowIfNull(options);
FileSavePicker fileSavePicker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = fileName,
};
foreach(var item in options.WindowsFileTypes)
{
fileSavePicker.FileTypeChoices.Add(item.Key, item.Value);
}
if(MauiWinUIApplication.Current.Application.Windows[0].Handler.PlatformView is MauiWinUIWindow window)
{
InitializeWithWindow.Initialize(fileSavePicker, window.WindowHandle);
}
StorageFile file = await fileSavePicker.PickSaveFileAsync();
if(file == null)
{
return false;
}
using(FileStream fileStream = new(file.Path, FileMode.OpenOrCreate))
{
fileStream.Write(bytes, 0, bytes.Length);
}
return true;
}
When PickSaveFileAsync is executed, the following dialog is displayed in Debug mode.
Please let me know what to do.
Executing following code on .NET MAUI causes the application to crash. public async Task<bool> ShowAsync(string fileName, byte[] bytes, SaveFilePickerOptions options)
{
ArgumentNullException.ThrowIfNull(options);
FileSavePicker fileSavePicker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = fileName,
};
foreach(var item in options.WindowsFileTypes)
{
fileSavePicker.FileTypeChoices.Add(item.Key, item.Value);
}
if(MauiWinUIApplication.Current.Application.Windows[0].Handler.PlatformView is MauiWinUIWindow window)
{
InitializeWithWindow.Initialize(fileSavePicker, window.WindowHandle);
}
StorageFile file = await fileSavePicker.PickSaveFileAsync();
if(file == null)
{
return false;
}
using(FileStream fileStream = new(file.Path, FileMode.OpenOrCreate))
{
fileStream.Write(bytes, 0, bytes.Length);
}
return true;
} When PickSaveFileAsync is executed, the following dialog is displayed in Debug mode.Please let me know what to do. Read More