在WPF中,Page中Window的Activated事件的等效或最接近匹配是什么?

There is Activated event in a Window in WPF. What is the the closest match for Activated event for a page. I want to use an event that triggers every time a page is displayed.

评论
  • 良德辉
    良德辉 回复

    There is the Loaded event which occurs when the element (Page) is laid out, rendered, and ready for interaction.

    The Frame class, which I guess is used to host your pages, also has a ContentRendered event that you can handle.

  • ueaque
    ueaque 回复

    如何使用IsVisibleChanged事件。

    在您的窗口中,您可以在整个应用程序中使用您的基类定义,也可以只是您感兴趣的基类定义。在其开头添加一个调用,例如

    public class MyBaseclassWindow : Window
    {
       public MyBaseclassWindow()
       {
          IsVisibleChanged += MyBaseclassWindow_IsVisibleChanged;
       }
    
       private void MyBaseclassWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
       {
          // e.NewValue will be TRUE when the visibility is coming back on
          if ( !e.NewValue )
             return;
    
          // do whatever you want every time window becomes visible.
       }
    }
    

    如果这是您希望在整个系统中完成的操作,则只需在创建表单时始终使用此窗口作为基本窗口即可。