playground
playground

Reputation: 75

Why is the window shown for a moment?

I am developing a WPF project in C#.

A SubWindow is created in the main window. Also, a LogWindow is created.

As shown, I hide mainWindow and subWindow:

SubWindow subWindow = new SubWindow();
LoginWindow loginWindow;
void MainWindow()
{
   InitializeComponent();

   subWindow.Visibility = Visibility.Hidden;
   this.Visibility = Visibility.Hidden;

   loginWindow = new LoginWindow();
   loginWindow.Show();
}

There is a problem here.

loginWindow is visible, but mainWindow and subWindow are shown momentarily. I do not want them to appear, even for a short moment.

Upvotes: 0

Views: 94

Answers (3)

faiz-e
faiz-e

Reputation: 210

Try setting the Main Window's visibility attribute to hidden by default in you XAML file.

Visibility="Hidden"

This might fix your issue, as the window will never appear at first place. You can then make it visible in your code whenever required.

Upvotes: 0

BeNes
BeNes

Reputation: 88

Remove StartupUri line in App.xaml, then move your code into App.xaml.cs making use of overriding OnStartup

protected override void OnStartup(StartupEventArgs e)
{
    MainWindow mainWindow = new MainWindow()
    {
        Visibility = Visibility.Hidden
    };

    SubWindow subWindow = new SubWindow()
    {
        Visibility = Visibility.Hidden
    };

    LoginWindow loginWindow = new LoginWindow();

    loginWindow.Show();

    base.OnStartup(e);
}

Upvotes: 2

EldHasp
EldHasp

Reputation: 7908

Your problem is not in this code. I duplicated this code and ran the Solution. The problem you described does not reproduce. Only LoginWindow is displayed without flickering other windows.

If you can, then reproduce the problem in a simple example Solution. And put it on GitHub or in the archive to run it and understand the reason.

Upvotes: 1

Related Questions