wpf为什么窗口在加载事件后显示

时间:2014-04-13 19:14:42

标签: wpf

主窗口出现后,我需要将某些内容写入屏幕。但实际情况是,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();
        }
    }

1 个答案:

答案 0 :(得分:2)

尝试使用 ContentRendered 事件。

加载窗口时调用

Loaded事件,而不显示任何内容,即只有空白窗口。

当窗口的实际内容在其上呈现时,会引发ContentRendered事件。


按以下顺序引发事件:

  1. SourceInitiated
  2. 激活
  3. 加载
  4. ContentRendered
  5. 您可以在此处阅读更多相关信息 - Window Lifetime events