Move the initilizeComponents() inside OnAppearing()

yccycc ✭✭USMember ✭✭

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.

Best Answer

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ US ✭✭✭✭✭
    edited November 2017 Accepted Answer

    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.

Answers

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    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.

  • yccycc ✭✭ USMember ✭✭

    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() ?

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭
    edited November 2017 Accepted Answer

    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.

  • yccycc ✭✭ USMember ✭✭

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

    Thanks

  • NMackayNMackay mod GBInsider, University mod

    @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.

  • yccycc ✭✭ USMember ✭✭

    Thanks for you answer.
    Can someone add his experience ?

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    edited December 2017
    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.
  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    @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

  • yccycc ✭✭ USMember ✭✭

    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 :)

  • batmacibatmaci ✭✭✭✭✭ DEMember ✭✭✭✭✭

    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.

  • batmacibatmaci ✭✭✭✭✭ DEMember ✭✭✭✭✭
    edited March 2018

    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();            
            }
       
    
  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    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.
  • NMackayNMackay mod GBInsider, University mod

    @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

  • batmacibatmaci ✭✭✭✭✭ DEMember ✭✭✭✭✭
    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
Sign In or Register to comment.