AvaloniaUtils / DialogHost.Avalonia

AvaloniaUI control that provides a simple way to display a dialog with information or prompt the user when information is needed
MIT License
228 stars 15 forks source link

Unable to open multiple dialogs one by one #4

Closed Gigas002 closed 2 years ago

Gigas002 commented 2 years ago

When I call DialogHost.Show method this way to open dialogs one by one (this is a ViewModel of dialog to show):

public async Task<DialogResult> ShowAsync()
{
    await DialogHost.DialogHost.Show(this, DialogHostId);

    return (DialogResult)await DialogHost.DialogHost.Show(this, DialogHostId);
}

The second call shows dialog for a frame of a second, then it automatically closes, but the interface still looks like "blocked" and the only way to get rid of this is to rerun the program. After digging inside of the second Show calls, I've found out, that it throws an exception in DialogHost.cs class after list.Count == 0 check on this method:

private static DialogHost.DialogHost GetInstance(string? dialogIdentifier)
{
  if (DialogHost.DialogHost.LoadedInstances.Count == 0)
    throw new InvalidOperationException("No loaded DialogHost instances.");
  List<DialogHost.DialogHost> list = DialogHost.DialogHost.LoadedInstances.Where<DialogHost.DialogHost>((Func<DialogHost.DialogHost, bool>) (dh => dialogIdentifier == null || object.Equals((object) dh.Identifier, (object) dialogIdentifier))).ToList<DialogHost.DialogHost>();
  if (list.Count == 0)
    throw new InvalidOperationException("No loaded DialogHost have an Identifier property matching dialogIdentifier ('" + dialogIdentifier + "') argument.");
  return list.Count <= 1 ? list[0] : throw new InvalidOperationException("Multiple viable DialogHosts. Specify a unique Identifier on each DialogHost, especially where multiple Windows are a concern.");
}

I should also mention, that if I manually set some delay between dialog's calls this way:

public async Task<DialogResult> ShowAsync()
{
    await DialogHost.DialogHost.Show(this, DialogHostId);

    await Task.Delay(1);

    return (DialogResult)await DialogHost.DialogHost.Show(this, DialogHostId);
}

Then it works as expected.

SKProCH commented 2 years ago

Hello, @Gigas002

This issue fixed in v0.3.1.