How to access the elements of a ControlTemplate in Xamarin Forms

后端 未结 3 1447
温柔的废话
温柔的废话 2021-01-12 13:58

I have a ControlTemplate defined in App.xaml. Now, I need to be able to handle certain UI events. In Visual Studio\'s XAML editor, if I attach a handler to an event, the han

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 14:16

    If you have Page instance you can cast it to IPageController and it's InternalChildren property contains controls from the template.

    Here is an extension method I'm using to find controls by name

    public static T FindTemplateElementByName (this Page page, string name)
        where T: Element
    {
        var pc = page as IPageController;
        if (pc == null)
        {
            return null;
        }
    
        foreach (var child in pc.InternalChildren)
        {
            var result = child.FindByName (name);
            if (result != null)
            {
                return result;
            }
        }
    
        return null;
    }
    

提交回复
热议问题