如何在Xamarin表单中访问ControlTemplate的元素 [英] How to access the elements of a ControlTemplate in Xamarin Forms

查看:209
本文介绍了如何在Xamarin表单中访问ControlTemplate的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App.xaml中定义了一个ControlTemplate.现在,我需要能够处理某些UI事件.在Visual Studio的XAML编辑器中,如果我将处理程序附加到事件,则在App.xaml.cs中创建该处理程序.但是,我需要在使用此控件模板的页面中执行此操作.

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 handler is created in App.xaml.cs. However, I need to do this in pages that use this control template.

我认为唯一的方法是遍历控件模板中的元素,找到合适的元素并在页面中创建处理程序.但是,我不确定如何从ControlTemplate对象访问元素.

I'm thinking the only way is to iterate through the elements in the control template and find the right element and create handlers in pages. However, I'm not sure how to access the elements from the ControlTemplate object.

Xamarin是否提供一种窥视ControlTemplate内容的方法?

Does Xamarin provide a way to peek into the contents of a ControlTemplate?

推荐答案

尽管这是一个老话题,但是实际的Xamarin Forms API提供了以下内容并且可以直接使用:

Although this is a old topic but the actual Xamarin Forms API provides the following and works out of the box:

contentView.GetTemplateChild("nameOfTheControl")

这篇关于如何在Xamarin表单中访问ControlTemplate的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆