PrismLibrary / Prism

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Xamarin Forms, and Uno / Win UI Applications..
Other
6.33k stars 1.64k forks source link

[BUG] dialog did not show up, when window in activated #3268

Closed hengway closed 4 days ago

hengway commented 5 days ago

Description

I'm using the Prism Library to manage dialogs with IDialogService. The issue I encountered is that when the main application window becomes inactive (deactivated) and a dialog is opened via ShowDialog(), the dialog is created but does not become visible or active. However, I can confirm that the dialog instance is created correctly, as its logic runs in the background, but the UI doesn’t show even the user manually focuses on the parent window at the later on.

Steps to Reproduce

  1. Invoke below code
    protected virtual async void OnAlert() 
    {
     await Task.Delay(5000);
     _dialogService.ShowDialog(ViewName.PopupAlert, new DialogParameters {{ "message", Texts.this_is_an_alert }}, (result ) => {
         Console.WriteLine(result.ToString());
     });
    }
    1. Open a browser, or any application to make the WPF application inactive.
    2. The dialog will not show

Platform with bug

WPF

Affected platforms

Windows

Did you find any workaround?

I did found a solution which is when invoke the dialog invoke Application.Current.MainWindow.Activate(); but this create an issue, which if I open a "Dialog B" from a "Dialog A", when the "Dialog B" close, the focus will go to the MainWindow but not the "Dialog A"

Relevant log output

No response

hengway commented 5 days ago

I found an alternative solution to the issue by placing the code below in the dialog's ViewModel constructor.

foreach (Window window in Application.Current.Windows)
{
    if (window.GetType().Name == "MainWindow" || window.GetType().Name == "DialogWindow") 
    {
        window.Activate();
    }
}

By refocus all the window in the window stack seem work.

brianlagunas commented 5 days ago

You did not provide a sample that reproduces the issue. However, I'm not sure this is a bug. This appears to be standard WPF behavior. The Prism WPF dialog service simply wraps showing a window from a VM and relies on WPF's default Window behaviors. Showing a dialog/window without user interaction and while the application does not have focus seems like a very strange scenario for me.

hengway commented 4 days ago

@brianlagunas Thank you for your response. While setting up the sample project, I finally identified the root cause—it was due to the window style applied to the dialog.

 <prism:Dialog.WindowStyle>
     <Style TargetType="{x:Type Window}">
         <Setter Property="WindowStyle" Value="ToolWindow" /> <--- the issue happen after apply this
     </Style>
 </prism:Dialog.WindowStyle>

Below is the sample code:

DialogBug.zip

And I had record video on the issue reproduce step https://github.com/user-attachments/assets/9833300d-4dbb-406d-93eb-9d7e1759aa2e

I applied the style to customize the dialog's appearance. However, after commenting it out, the issue seems gone.

I believe the bug may originate from the window behavior. This issue occurs in my payment processing application when a user switches to another application during a transaction, and the success or error dialog fails to appear at the end.

Anyway thank you for the framework.

brianlagunas commented 4 days ago

Glad you figured it out. Thanks for sharing your solution for others.