【问题标题】:wpf why window showing up after loaded eventwpf为什么加载事件后出现窗口
【发布时间】:2014-05-27 15:41:11
【问题描述】:

在主窗口出现后,我有一些东西需要写入屏幕。但实际情况是 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();
        }
    }

【问题讨论】:

  • 试试ContentRendered事件。
  • tnx.工作完美。但是它们之间有什么区别呢?
  • 关于区别,看我的answer

标签: wpf


【解决方案1】:

尝试使用 ContentRendered 事件。

Loaded 事件在加载窗口时调用,但没有在其上呈现任何内容,即只有空白窗口。

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


事件按以下顺序引发:

  1. SourceInitiated
  2. 已激活
  3. 已加载
  4. 内容渲染

您可以在此处阅读更多相关信息 - Window Lifetime events

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多