I have a DialogViewModel
class with async Task LoadData()
method. This method loads data asynchronously and shows this dialog, which notifies user about loading. Here is the code:
try
{
var dialog = new DialogViewModel();
var loadTask = dialog.LoadData();
WindowManager.ShowDialog(dialog);
await loadTask;
}
catch (Exception ex)
{
Logger.Error("Error in DialogViewModel", ex);
// Notify user about the error
}
When LoadData
throws an exception, it isn't handled until user exits the dialog. It happens because exception is handled when calling await
, and it's not happening until WindowManager.ShowDialog(dialog)
completes.
What is the correct way to show a dialog with async loading? I've tried this ways:
- Call
LoadData()
inOnShow()
, constructor or similar. But this won't work if I'll need to show this dialog without any data - Call
await LoadData()
before showing the dialog. This way user have to wait for data to load before actually seeing the window, but I want the window to show up instantly with a loading indicator.