WPF: What is between the Initialized and Loaded event?

前端 未结 3 762
悲哀的现实
悲哀的现实 2021-02-02 11:30

I want to run some code when the Window or Control is first displayed. I can\'t use Loaded because that can fire more than once. I can\'t use Initialized because that is done by

3条回答
  •  太阳男子
    2021-02-02 11:52

    If you don't want to use the boolean method of doing things, then you can create a method and subscribe to the Loaded event with it, then unsubscribe at the end of that method.

    Example:

    public MyDependencyObject(){
      Loaded += OnLoaded;
    }
    
    private void OnLoaded(object sender, EventArgs args){
      DoThings();
      Loaded -= OnLoaded;
    }
    

提交回复
热议问题