Hi @Rajashekaraiah, Bhadresh B SBOBNG-PTIY/ER ,
Thanks for reaching out.
- ContentDialog behaving like a modal:
To make sure a ContentDialog truly blocks the background window:
- Always use
await myDialog.ShowAsync()instead ofShow(). - Make sure the dialog is attached to the current, fully initialized window.
- Avoid showing dialogs too early in the app startup; wait until the window is ready.
Sometimes, even with ShowAsync(), the background may still be clickable if the dialog isn’t attached correctly or if multiple windows exist. If your dialog is triggered from a ViewModel, consider passing the window reference or raising an event to show the dialog from the View code.
- Application launch performance and freezing:
Slow startup and temporary freezes usually happen when heavy initialization runs on the main UI thread. You can improve responsiveness by:
- Offloading heavy tasks to background threads using
async/awaitorTask.Run. - Showing the main window only after essential UI components are ready.
- Profiling startup with Visual Studio’s performance tools to identify bottlenecks.
- Handling the close button (‘X’):
WinUI 3 doesn’t provide a direct close event by default, but you can use the window’s Closing event or the AppWindow API to intercept closing. In that event, you can validate data or show a ContentDialog to confirm closing. If needed, cancel the close by setting args.Cancel = true.
Here’s a reference example that combines all three ideas (keep in mind, this is just a starting point, you’ll want to adapt it to your MVVM setup and project structure):
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Hook into Closing event
this.Closing += MainWindow_Closing;
// Start async initialization
InitializeAsync();
}
private async Task InitializeAsync()
{
// Simulate heavy startup work
await Task.Run(() => {
Thread.Sleep(2000); // replace with real initialization
});
// Show main window content
}
private async void MainWindow_Closing(object sender, WindowClosingEventArgs args)
{
// Show confirmation dialog before closing
var dialog = new ContentDialog
{
Title = "Confirm Exit",
Content = "Do you want to exit?",
CloseButtonText = "No",
PrimaryButtonText = "Yes"
};
var result = await dialog.ShowAsync(); // properly attached to window
if (result != ContentDialogResult.Primary)
{
args.Cancel = true; // prevent closing
}
}
private async void ShowModalDialog()
{
var dialog = new ContentDialog
{
Title = "Hello",
Content = "This is a modal dialog.",
CloseButtonText = "OK"
};
await dialog.ShowAsync(); // ensures modal behavior
}
}
Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.