0

最近我开始使用 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.
            }
        }
    }
}

谢谢

4

2 回答 2

1

我不能使用 Element.Text 属性,因为我将删除文本并将图像设置为我的按钮。我需要使用 Tag 或 Name 属性

Name是在Render处理阶段分配的,对于android平台,就像我说的,通常我们id用来标识控件。这里似乎无法公开 name 属性,解决方法是我们可以通过注册一个BindableProperty. 例如:

public class MyButton : Button
{
    public string ButtonName
    {
        get { return (string)GetValue(ButtonNameProperty); }
        set { SetValue(ButtonNameProperty, value); }
    }

    public static readonly BindableProperty ButtonNameProperty =
        BindableProperty.Create(
            propertyName: "ButtonName",
            returnType: typeof(string),
            declaringType: typeof(MyButton),
            defaultValue: null,
            propertyChanged: OnButtonNameChanged);

    private static void OnButtonNameChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }
}

后面的代码:

public class MyButtonRenderer : ButtonRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
    {
        base.OnElementChanged(e);
        //Element.Id

        if (Element != null)
        {
            var element = Element as MyButton;
            var name = element.ButtonName;
        }
    }
}

你代码中的一个小问题是Control自定义渲染器中的 表示各个平台的原生控件,Element是你自定义的控件,所以我Element这里使用了属性。

并使用此控件:

<local:MyButton x:Name="signInButton" Text="custom button" ButtonName="signInButton" />

我知道当你使用这个 custom 时设置 name 属性两次很烦人Button,但这是我现在找到的从渲染器获取每个自定义按钮的标签的唯一方法。如果您不需要Button在 PCL 后面的代码中访问它,那么也许您不需要设置该x:Name属性。

于 2017-02-15T09:11:59.357 回答
0

首先,我不确定你有没有,你需要在你的命名空间顶部添加这行代码:

[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]

其次,在您的代码中,您可以访问 Element 本身,例如:

if (control != null)
{
    string buttonText = Element.Text; //In your posted case it will be equal to "Sign In"
}

我不确定您是否可以实际x:Name从内部访问该属性,CustomRenderer因为它是类变量名称。但是根据您要执行的操作,您拥有可以使用的所有Element对象。

编辑

我找不到直接访问 name 属性的方法,但是您可以使用一种解决方法。
在您的 PCL 项目和 CustomButton 类中,您可以添加所需的属性:

public class CustomButton : Button
{
    public string Name { get; set; }
    //rest of your code
}

在您的 xaml 页面中,您现在可以将其添加为属性,例如:

<local:CustomButton x:Name="signInButton" Name="signInButton" Text="Sign In" Clicked="OnLoginButtonClicked"  TextColor ="White" FontSize="15" FontAttributes="Bold"/>

之后,在我之前向您展示的渲染器中,您现在可以这样做:

if (Control != null)
{
    string buttonName = ((CustomButton)Element).Name; //it will have the "signInButton" now.
}
于 2017-02-15T08:35:52.337 回答