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

查看:28
本文介绍了如何在 Xamarin Forms 中访问 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 Forms 中访问 ControlTemplate 的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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