Reputation: 1758
Consider the following code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("MyMessage");
}
If I am trying to display a message box after a WPF window has been loaded, when I run the application, the WPF window is displayed with a transparent background (only the non-client area is visible) and it takes 3-5 seconds until the message box appears. The WPF window returns to normal only after the message box has been closed.
Is this normal? Does anyone else experience this?
EDIT: I have added a screenshot of how the window looks like:
Upvotes: 3
Views: 2277
Reputation: 132628
The MessageBox
is getting shown at the Normal
DispatcherPriority, which occurs before things like DataBind
, Render
, and Loaded
, so the code that initializes your Window's objects is not getting run until after you dismiss the MessageBox
You can fix this by simply showing the MessageBox
at a later DispatcherPriority, such as Background
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitializeComponent();
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate() { MessageBox.Show("MyMessage"); }));
}
Upvotes: 7