I write some data to a file that the user can choose with a File Picker. Here my test code with some nonsense data:
private async void DoExportAsync()
{
// write some data to a temporary file
string tempPath = Path.Combine(FileSystem.CacheDirectory, Guid.NewGuid().ToString() + ".tmp");
try
{
using (var writer = new StreamWriter(tempPath))
{
writer.WriteLine("Test");
writer.Flush();
}
// Now copy content to the final file that the user selects
string? finalPath = await FileUtils.SaveFileAsync(tempPath, "PackerData", ".txt");
// Display a message on success
if (finalPath != null)
await App.Current.Windows[0].Page!.DisplayAlert("Export", $"Gespeichert unter:\n{finalPath}", "OK");
}
finally
{
// delete the temporary file
if (File.Exists(tempPath))
File.Delete(tempPath);
}
}
FileUtils.SaveFileAsync calls
await CommunityToolkit.Maui.Storage.FileSaver.Default.SaveAsync(...)
When I am debugging this code, the debugger detaches when the using { } block is left. After that the file picker still appears and writes the file, but the file is empty. My app disappears from the screen. DisplayAlert is not executed.
However, this code works well in the Android Emulator when no debugger is attached. (I did not yet test the behaviour on a real smartphone.)
Any idea how to work around this problem?
await using (StreamWriter writer = new StreamWriter(tempPath))the debugger detaches later, atawait CommunityToolkit.Maui.Storage.FileSaver.Default.SaveAsync(...)