Search code examples
xamarin.iosxamarin.androidxamarin.formscustom-renderer

How to access object name in custom classes using Xamarin.forms?


Recently I have started development using Xamarin.Forms. I am creating an application in which I have to customize the button, so I created customButton class. Now, I want to access the button in custom class using object.name property but I am not able to do that. Can anyone suggest how I can do that.

Code

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

CustomButton class inside Android project

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.
            }
        }
    }
}

Thanks


Solution

  • I can't use the Element.Text property as I will remove the text and set image to my button. I need to use either Tag or Name property

    The Name is assigned at the Render processing stage, for android platform, as I said, usually we use id to identify the control. It seems the name property cannot be exposed here, a workaround is that we can expose a property by registering a BindableProperty. For example:

    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)
        {
        }
    }
    

    Code behind:

    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;
            }
        }
    }
    

    A little problem in your code is that the Control in the custom renderer indicates the native control of each platform, the Element is your custom control, so I used Element property here.

    And to use this control:

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

    I know it is annoying to set the name property twice when you use this custom Button, but it's the only method I found for now to get a tag for each custom button from renderer. If you don't need to access this Button in code behind in PCL, then maybe you don't need to set the x:Name property.