Allow showing User Prompt without any Owner

Fundamentals for Avalonia UI Forum

Posted 3 months ago by John Smith
Avatar

There are some cases when you need to show dialog window before any other window of your program opened. For example, if an exception was thrown when loading the program. There is example of User Prompt displaying Exception message in your Samples app, but as far as I understand it can't be shown without any parent window.

Comments (1)

Posted 3 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi John,

Great question.  In Avalonia, calling Window.ShowDialog without an owner will throw an exception because a dialog must have an owner.  Without an owner, you can show a window but it would not be modal.  We currently only support ShowDialog and require an owner because Avalonia requires it.

Note that attempting to show a User Prompt on non-desktop platforms, like a browser using WASM, would not be possible without an owner to host the overlay that displays the prompt.

The scenario you're describing is pretty much the only scenario I can think of where you would not have a window available to be the owner.  There are a few reasons why this may not work as expected even if we supported Show (without an owner):

  1. Any fatal exception thrown during application startup will cause the application to terminate, and any non-blocking calls to show a window at this time would likely see the running process terminated before the window could even be shown.
  2. If application startup has failed, that likely means application resources are also not properly initialized and the User Prompt would not have the styles and templates necessary to properly display information.
  3. If the main window fails to load, I honestly don't even know if Avalonia would support loading/showing a separate window.

In that specific scenario, I think logging exceptions may be the most consistent way to ensure startup exceptions are captured.  If a non-fatal exception is detected during application startup, the exception details could be cached in a global variable, allow startup to continue, and then display any cached exceptions after the main window loads.

If you wanted to experiment, you could try hosting our UserPromptControl inside a standard Window and show it without an owner using code like this:

try {
  // ...
}
catch (Exception ex) {
  // Build the UserPromptControl configured for the exception
  var userPromptControl = UserPromptBuilder.Configure()
    .ForException(ex)
    .BuildWithoutShowing();

  // Show the control in your own window
  var window = new Window() {
    Content = userPromptControl
  };
  window.Show();
}


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.