TemplatedView should have an OnApplyTemplate virtual method to be overriden by custom controls

AndrewMobileAndrewMobile USMember ✭✭✭✭
edited November 2017 in Xamarin.Forms


Add a virtual OnApplyTemplate() metod to TemplateView class, so that TemplateView derived classes (custom controls) know when template was rendered and it's available.

API Changes

public class TemplatedView : ...
       protected virtual void OnApplyTemplate()

Intended Use Case

In Xamarin Forms, there's no simple\clean way to know when a custom control's ControlTemplate was applied so that the custom control can get access to its controls. I'm not saying it's not possible, but I wish there was a method as elegant as in Windows XAML.

For anyone familiar with Windows XAML, the Control class has an OnApplyTemplate: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement#Windows_UI_Xaml_FrameworkElement_OnApplyTemplate


Open · Last Updated


  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    you probably can listen for Content or ControlTemplate properties changed by overriding OnPropertyChanged()

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited November 2017


    About Content property: My custom controls are deriving from TemplatedView, not from ContentView, so there's no Content property. I really want a fully encapsulated custom control, deriving from ContentView is not ideal.

    Second, listening and comparing for property changes is both inefficient and doesn't work:

            protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
                if(propertyName == nameof(ControlTemplate))
                    var x = this.Children; // There's zero children at this point, most probably because children are not realized yet
Sign In or Register to comment.