【发布时间】:2017-02-15 17:11:59
【问题描述】:
最近我开始使用 Xamarin.Forms 进行开发。我正在创建一个应用程序,我必须在其中自定义按钮,所以我创建了 customButton 类。现在,我想使用 object.name 属性访问自定义类中的按钮,但我无法做到这一点。谁能建议我如何做到这一点。
代码
<local:CustomButton x:Name="signInButton" Text="Sign In" Clicked="OnLoginButtonClicked" TextColor ="White" FontSize="15" FontAttributes="Bold"/>
Android 项目中的CustomButton 类
namespace Sample.Droid
{
class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
//Want to access button name in if condition, instead of Control.
//Hope this will help you all in understanding my problem.
}
}
}
}
谢谢
【问题讨论】:
-
尽量确保将您的自定义类放在 interface 上,并添加对它的引用
-
“使用 object.name 属性访问自定义类中的按钮”是什么意思,您想在自定义渲染器中获取按钮的名称属性吗?例如,如果您在 android 项目中创建自定义
ButtonRenderer,通常我们使用ID来标识控件,而不是名称,因此它是特定于平台的,那么您要在哪个平台上获取名称属性?还是只是想获取自定义渲染器类名? -
我正在尝试在 Android 和 iOS 平台上使用 x:Name="signInButton" 属性,我有 4 个按钮,我想自定义每个按钮。所以,我正在考虑为此使用 if 或 switch case。但在 CustomButton 类中,我无法根据 name 属性访问我的按钮。
-
显示您编写的代码将帮助我们所有人首先准确了解您的问题,并更好地帮助您。例如,您是否使用自定义渲染器?您是否尝试从 Android 项目内部访问 PCL 中的对象名称?
-
我已经添加了代码,虽然格式有点错误。是的,我正在尝试访问 Android 项目中的对象名称。
标签: xamarin.ios xamarin.android xamarin.forms custom-renderer