0

I have a window, it will do some checking before it is shown.

public class MyDlg : Window
{
    public MyDlg()
    {
        Initialized += new EventHandler(Window_Initialized);
    }
    private void Window_Initialized(object sender, EventArgs eventArgs)
    {
        if (!/*do some checking*/)
        {
            Loaded += (s, e) => Close();
        }
    }
}

If "do some checking" fail, the above code will close my window immediately after the window is loaded. However this is too late because I can see the window just appear and disappear.

How can I close my window without showing it?

EDIT: The one who will construct MyDlg is like:

MyDlg dlg = new MyDlg ();
dlg.ShowDialog();

But it is hard for me to prevent calling 'ShowDialog()', because they are written by other people (I'm trying to write MyDlg in some library)

Dia
  • 851
  • 1
  • 15
  • 35
  • Who constructs MyDlg ? and in addition, use google http://stackoverflow.com/questions/1539958/wpf-showing-dialog-before-main-window – ilansch Apr 19 '17 at 07:52
  • 2
    Why let it open? Can't you restrict it even before opening ? – Naresh Ravlani Apr 19 '17 at 07:52
  • i'd rather move the check to be within Load event. – Lei Yang Apr 19 '17 at 07:56
  • In some MVVM implementations the ViewModel is created after the View so you'd like to not show the View if the ViewModel creation throws an exception. Usually though, the ViewModel has been created before Show is called – Paul McCarthy Feb 04 '20 at 16:52

2 Answers2

1

How can I close my window without showing it?

Perform the check before calling the Show or ShowDialog method of the window. You could either do this in the calling code:

MyDlg dlg = new MyDlg();
//perform your check here...
dlg.ShowDialog();

...or in the constructor of the MyDlg window:

public MyDlg()
{
    //perform your check here...
}

Obviously the window is already shown by the time the Window_Initialized event handler gets invoked so then it is too late to perform any check if you don't want the window to appear. You cannot close a window that hasn't been opened.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

You can create splash dialog inside your new window.

And set IsEnabled=False on window/dialog.

Or if your operation is quick then there is no need for splash. Just hide your window:

<Window x:Class="Wpf.Test01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf.Test01"
        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        mc:Ignorable="d"
        IsEnabled="False"
        WindowStyle="None"
        AllowsTransparency="True"
        Title="MainWindow" Height="350" Width="525">
    <Window.Background>
        <SolidColorBrush Opacity="0.0" Color="White" />
    </Window.Background>

You can see it done here WPF Window with transparent background containing opaque controls

Of course change the properties back to visible/default if everything is ok

Community
  • 1
  • 1
Matija K.
  • 185
  • 1
  • 8