locked
how can I init my custom control RRS feed

  • Question

  • Hi everybody

                How can I implement InitializeComponent function in my control?

    I want put the following code into constructor but the GetTemplateChild retun null. How can I load the Xaml ?

            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                htmlContainer = base.GetTemplateChild("htmlContainer") as WebView;
                historyContainer = base.GetTemplateChild("historyContainer") as ListView;
            }

    Saturday, January 12, 2013 5:34 AM

Answers

  • That's perfectly normal. Controls are not fully rendered in OnNavigatedTo. You would want to wait for the "Loaded" event for that. However you should also design your control to be able to handle this if possible (ie you should be able to set properties prior to the control has loaded, and in OnApplyTemplate you can set it's state based on these properties).

    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    • Marked as answer by L.K9 Thursday, January 17, 2013 11:27 AM
    Thursday, January 17, 2013 4:27 AM

All replies

  • Did you remember to set the DefaultStyleKey in the constructor?

    Also you said you put the following code in the constructor, but it's an entire method. Do you mean you call OnApplyTemplate from your constructor? You should never call OnApplyTemplate directly. If you set DefaultStyleKey and you have the template properly defined in \Themes\Generic.xaml OnApplyTemplate will automatically get called. Provided there's an element <WebView x:Name="htmlContainer" /> inside your template, this should then work.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net


    • Edited by Morten NielsenMVP Saturday, January 12, 2013 11:05 PM
    • Proposed as answer by Aaron Xue Tuesday, January 15, 2013 7:49 AM
    Saturday, January 12, 2013 11:05 PM
  • Did you remember to set the DefaultStyleKey in the constructor?

    Also you said you put the following code in the constructor, but it's an entire method. Do you mean you call OnApplyTemplate from your constructor? You should never call OnApplyTemplate directly. If you set DefaultStyleKey and you have the template properly defined in \Themes\Generic.xaml OnApplyTemplate will automatically get called. Provided there's an element <WebView x:Name="htmlContainer" /> inside your template, this should then work.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net


      thanks Morten Nielsen .I have already do that but the OnApplyTemplate method is called later than OnNavigateTo of MainPage.So When Users

    use my control in OnNavigateTo method my control is not initialized it will throw null reference exception(historyContainer and html Contrainer is null )

    Thursday, January 17, 2013 2:40 AM
  • That's perfectly normal. Controls are not fully rendered in OnNavigatedTo. You would want to wait for the "Loaded" event for that. However you should also design your control to be able to handle this if possible (ie you should be able to set properties prior to the control has loaded, and in OnApplyTemplate you can set it's state based on these properties).

    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    • Marked as answer by L.K9 Thursday, January 17, 2013 11:27 AM
    Thursday, January 17, 2013 4:27 AM
  • That's perfectly normal. Controls are not fully rendered in OnNavigatedTo. You would want to wait for the "Loaded" event for that. However you should also design your control to be able to handle this if possible (ie you should be able to set properties prior to the control has loaded, and in OnApplyTemplate you can set it's state based on these properties).

    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    Thank you very much . It works fine.
    Thursday, January 17, 2013 11:26 AM