locked
Move the initilizeComponents() inside OnAppearing() RRS feed

  • Question

  • User360745 posted

    Hi,

    Little question, i was trying to optimise a TabbedPage of 3 pages.

    To make it more fluid i have moved the call of initializeComponents() from the constructor to an overide of OnAppearing().

    It works corectly but i have a doubt if it's an acceptable practice or not ?

    Thanks.

    Wednesday, November 29, 2017 5:28 PM

Answers

  • User180523 posted

    So except for this point their is, in theory, no problem with the usage of InitializeComponent() into OnAppearing() ?

    Hard to say what will happen when you deviate from standard practices. In 15+ years of XAML based development I've never worked on a project where it was done. Personally, to me, that would be a hint.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 30, 2017 1:19 PM

All replies

  • User180523 posted

    The same instance of a page or control could appear many times. Like every time the app is put in background and brought forward again. No need to do all this work every time it appears if just once in the constructor is sufficient.

    Wednesday, November 29, 2017 6:05 PM
  • User360745 posted

    Thanks for your answer.

    In my previous situation the constructor was called with the inialization of the tabbed page, so the 3 pages was created everytimes. But only the first tab contains major informations, the two others contains secondary information and they will not be display very frequently, so this seem to be an acceptable compromise because she will be load again anyway if the user quit the tabbed page and open it again.

    So except for this point their is, in theory, no problem with the usage of InitializeComponent() into OnAppearing() ?

    Thursday, November 30, 2017 8:06 AM
  • User180523 posted

    So except for this point their is, in theory, no problem with the usage of InitializeComponent() into OnAppearing() ?

    Hard to say what will happen when you deviate from standard practices. In 15+ years of XAML based development I've never worked on a project where it was done. Personally, to me, that would be a hint.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 30, 2017 1:19 PM
  • User360745 posted

    Ok, i will see if we keep this way or back to a more standard method.

    Thanks

    Thursday, November 30, 2017 1:22 PM
  • User76049 posted

    @ClintStLaurent said:

    So except for this point their is, in theory, no problem with the usage of InitializeComponent() into OnAppearing() ?

    Hard to say what will happen when to deviate from standard practices. In 15+ years of XAML based development I've never worked on a project where it was done. Personally, to me, that would be a hint.

    Was thinking exactly the same thing.

    Thursday, November 30, 2017 1:41 PM
  • User360745 posted

    Thanks for you answer. Can someone add his experience ?

    Friday, December 1, 2017 4:38 PM
  • User350 posted

    I'd say this is a bad idea. What happens if you push to another page and pop back to this page? OnAppearing will be called and InitializeComponent will be called again on the existing page.

    Saturday, December 2, 2017 7:42 AM
  • User180523 posted

    @JamesLavery said: I'd say this is a bad idea. What happens if you push to another page and pop back to this page? OnAppearing will be called and InitializeComponent will be called again on the existing page.

    Yeah- I brought that up to. But the OP didn't seem to mind if that happened.

    @ClintStLaurent said: The same instance of a page or control could appear many times. Like every time the app is put in background and brought forward again. No need to do all this work every time it appears if just once in the constructor is sufficient.

    @ycc said: so this seem to be an acceptable compromise

    Monday, December 4, 2017 1:07 PM
  • User360745 posted

    I think i will check for a way to maintain a good balance between loading time and reload of data with a more traditional use of initializeComponents();

    Thanks for your answer :)

    Monday, December 4, 2017 5:25 PM
  • User176749 posted

    is there any flag to check IsInitiliazed or not? because in theory this is a good idea, especially having tabbed page or master detailpage in order to gain some start up time. I am using freshmvvm and using masterdetail page it preinitializes each pages on masterdeail load. if I do this, i gain around 2 secs but it seems that things are created again as others said and it crashes on UWP telling that there is already a child.

    Saturday, March 3, 2018 1:30 PM
  • User176749 posted

    you can just do it as below. this will insure that it will be initialized only once

     protected override void OnAppearing()
        {
            if(this.Content==null)
            {
                InitializeComponent();            
            }
    
    Saturday, March 3, 2018 1:35 PM
  • User350 posted

    Still a bad idea. InirializeComponent is designed to be called in the page constructor. You are coding a bad pattern if you're having to resort to tricks like this.

    Saturday, March 3, 2018 3:36 PM
  • User76049 posted

    @JamesLavery said: Still a bad idea. InirializeComponent is designed to be called in the page constructor. You are coding a bad pattern if you're having to resort to tricks like this.

    +1

    Saturday, March 3, 2018 4:55 PM
  • User176749 posted

    I am sorry but i am not seing so far anything bad in it. You can try on your project. I have even feeling that my pages are loading faster. What is the technical reason that it is bad beside traditions

    Sunday, March 4, 2018 1:10 AM