Move your code to Window_Loaded
event instead of calling it from constructor -
<Window Loaded="Window_Loaded"/>
Code behind -
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Your code here
}
Since, your window is not loaded yet as the code execution haven't get passed the constructor yet and in the meanwhile error message pops up. So, once the window gets loaded it will comes over the message box.