DockableBase's closing design should support async #247
-
The current signature of DockableBase.OnClose does not lend itself well to public virtual bool OnClose() This is problematic when prompting the user to save/discard/cancel a modified Is there an alternative, intended approach that I'm missing here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you can propose design solution it can be added in next major release. |
Beta Was this translation helpful? Give feedback.
-
Sorry for the non-response, but I don't know how to design an API that lends itself well to both sync and async scenarios. Not in a way that can be consumed by public virtual async Task<bool> OnCloseAsync() I ended up taking the approach from the public partial class DockableEditorViewModel : Document
{
[ObservableProperty] private ResourceEditorBaseViewModel _editor;
private readonly EditorsViewModel _editors;
public DockableEditorViewModel(ResourceEditorBaseViewModel editor, EditorsViewModel editors)
{
_editor = editor;
_editors = editors;
}
public override bool OnClose()
{
using var cts = new CancellationTokenSource();
bool result = default;
_editors.RequestSaveUserChanges(_editor, true).ContinueWith(x =>
{
result = x.IsCompletedSuccessfully && x.Result != UserSaveAction.Cancel;
cts.Cancel();
},
TaskScheduler.FromCurrentSynchronizationContext());
Dispatcher.UIThread.MainLoop(cts.Token);
return result;
}
} |
Beta Was this translation helpful? Give feedback.
Sorry for the non-response, but I don't know how to design an API that lends itself well to both sync and async scenarios. Not in a way that can be consumed by
Dock
internally anyways. I would want to override an API like:I ended up taking the approach from the
SyncDialogExtensions
before and integrating them into the VM itself at the cost of directly referencing an Avalonia bit (Dispatcher
). The VM is fairly isolated and is already tied toDock
which is tied to Avalonia, so I don't mind the break in MVVM purity too much. With AvalonDock, you didn't need wrappers like this.