Forum Cross Platform with Xamarin

XAML code behind x:name property is unaccessable

I'm new to xamarin forms, I created an Cross Paltform app. But when i give x:Name="" to any element in xaml. In code behind i can't access this property id. - I updated nuget packages even i keep trying to rebuild projects. But still unable to access xaml ids in code behind.

Tagged:

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can try to rebuilt your app after you give x:Name="" to an element in xaml.

    For example, when you add an Label in xaml(MainPage.xaml) as follows:

      <Label x:Name="mLabel" Text="Name" />
    

    You can access the label like this in this MainPage.xaml.cs:

        public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            mLabel.Text = "change text";
        }
    }
    

    Note: When you give x:Name="" to an element in file xaml in a page, you can only access this element in this page's xaml.cs.

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can try to rebuilt your app after you give x:Name="" to an element in xaml.

    For example, when you add an Label in xaml(MainPage.xaml) as follows:

      <Label x:Name="mLabel" Text="Name" />
    

    You can access the label like this in this MainPage.xaml.cs:

        public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            mLabel.Text = "change text";
        }
    }
    

    Note: When you give x:Name="" to an element in file xaml in a page, you can only access this element in this page's xaml.cs.

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @Jumi_ , have you resolved your problem?

Sign In or Register to comment.