WPF, Bad practice to put code directly after InitializeComponent?

后端 未结 2 1513
梦谈多话
梦谈多话 2021-01-21 11:47

I was just wondering about something. I have a frame that loads pages and currently each page has a Page_Loaded method that will run each time the page is accessed

相关标签:
2条回答
  • 2021-01-21 12:00

    As you point out the Page_Loaded event will be fired each time the page is refreshed, so if you want the code to only be executed the once, then putting it in the constructor is the logical place.

    Depending on how much code you need you might want to consider refactoring it into another method - but that's purely a matter of personal taste (or perhaps following coding standards).

    UPDATE

    I would guess that as the query used to generate tehBuildings is returning null (stating the obvious) then there's some other code you need to call before calling getBuildings. Without seeing your original code I wouldn't like to say what's now missing.

    0 讨论(0)
  • 2021-01-21 12:18

    It's legitimate to do something in the constructor. I think this is fine.

    WPF isn't quite like ASP.NET in terms of accessibility of objects / etc. It's a bit looser and so doing something in a constructor isn't quote the no-no it is in ASP.NET.

    0 讨论(0)
提交回复
热议问题