cs adding entries and giving them name

Cem Yedikardes 1 Reputation point
2021-08-22T20:15:05.21+00:00

I try to write like under but it not acceptable. are there anyway to make new entries with name?

{
InitializeComponent();
for (int i = 0; i < 2; i++)
{
Entry aentry = new Entry
{
Name = "aentry" + (i +1)
};
stack.Children.Add(aentry);
};
}

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,298 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2021-08-23T01:55:09.213+00:00

    Hi CemYedikardes-5780,

    Welcome to our Microsoft Q&A platform!

    There is no such property or method to set x:Name for Xamarin controls. In essence, x:Name is only used to help the compiler recognize the control.

    If you want to identify a control programmatically, you can use the property StyleId. According to documentation:Element.StyleId Property, we know that it can be used to set a user defined value to uniquely identify the element.

    for (int i = 0; i < 2; i++)  
    {  
        Entry aentry = new Entry  
        {  
            StyleId = $"Entry{i}",  
        };  
        stack.Children.Add(aentry);  
    };  
    

    Then you can use the following code to get the specified control based on the StyleId.

    var theEntry = stack.Children.ToList().Find(item => item.StyleId == "Entry1");  
    Console.WriteLine((theEntry as Entry).Text);  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.