Stav Alfi
Stav Alfi

Reputation: 13923

wpf why window showing up after loaded event

I have something that need to be written to the screen AFTER the main window is showing up. But the reality is that the Loaded event code is fully excuted and only then the window is up. What I need to do to change it?

*I dont care to use any other event but "Loaded".

public partial class MainWindow : Window
    {
        public const string UpdatedClientComputerURL = @"net.tcp://localhost:8181/";
        public const string UpdatedClientComputerHostName = "Client1";
        public MainWindow()
        {
            InitializeComponent();
            ClientAdministration.ConnectedToServerEvent += ClientAdministration_ConnectedToServerEvent;
            ClientAdministration.RestoredDownloadsEvent += ClientAdministration_RestoredDownloadsEvent;
            StatusLabel.Text = "Connecting to server...";
        }

        void ClientAdministration_RestoredDownloadsEvent()
        {
            StatusLabel.Text = "Opening TorrentS...";
        }

        void ClientAdministration_ConnectedToServerEvent(bool Conncted)
        {
            if(Conncted)
                StatusLabel.Text = "Connected to server...";
            else
                StatusLabel.Text = "Failed connect to server...";
            Task.Delay(5000).Wait();
            StatusLabel.Text ="Restoring downloads...";
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

            ///***I want the window to be open befor this code get excuted!!!!!***

            ClientAdministration.ConnectToServer(UpdatedClientComputerURL, UpdatedClientComputerHostName);
            ClientAdministration.RestoreDownloads();
        }
    }

Upvotes: 0

Views: 508

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

Try using ContentRendered event.

Loaded event is called when window is loaded without any content rendered on it i.e. only blank window.

Whereas ContentRendered event is raised when actual content of window gets rendered on it.


Events are raised in following order:

  1. SourceInitiated
  2. Activated
  3. Loaded
  4. ContentRendered

You can read more about it here - Window Lifetime events.

Upvotes: 4

Related Questions