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)

4
  • Who constructs MyDlg ? and in addition, use google stackoverflow.com/questions/1539958/… Commented Apr 19, 2017 at 7:52
  • 2
    Why let it open? Can't you restrict it even before opening ? Commented Apr 19, 2017 at 7:52
  • i'd rather move the check to be within Load event. Commented Apr 19, 2017 at 7: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 Commented Feb 4, 2020 at 16:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, maybe force checking before ShowDialog() is the best fix.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.