How to set label name in Xamarin Forms

I am trying to add a name to a shortcut to find it using the Content.FindByName <> () method

as far as I can tell that the Xamarin.Forms.Label () class does not have a Name property

Here is my page class

public class HelloWordCodePage : ContentPage { public Label HelloWorldLabel; public HelloWordCodePage() { HelloWorldLabel = new Label{Text = "Hello World", }; ConstructView(); } public void ConstructView() { Content = new StackLayout { Children = { HelloWorldLabel } }; } } 
+8
source share
5 answers

If you define your shortcut in Xaml, you can use this syntex:

 <Label x:Name="YourLableName" Text="A simple Label" /> 

And then access it in code like this

 YourLableName.Text = "bla bla" 

However, if you are not using Xaml, and the entire definition of your page is found in the code behind, you must save the link to this label to access it, instead of finding it using Content.FindByName<>() as @Sten Petrov commented

+9
source

I believe this name is intended to be used when creating an element in XAML. If you look at inheritance, you will find Element at the bottom, and it explicitly implements INameScope. You cannot use Label for INameScope, as this is the internal interface:

 namespace Xamarin.Forms { internal interface INameScope { object FindByName(string name); void RegisterName(string name, object scopedElement); void UnregisterName(string name); } } 
+1
source

The name is assigned during the Render processing phase. There is currently no need to know the display name when you are working on cross-platform ContentPage .

There is a GUID, but as you say, there is no ID. In my opinion, you would never want to get the platform identifier for the created control at this high level of page design. If you do, you may need to create the PageRenderer platform instead

When you start writing CustomRenderers and creating platform controls, you can assign identifiers if you want any custom implementations of the specific management platform that you want.

Remember the current ContentPage . You have a reference to a class object with a class called HelloWorldLabel , which you can use to change its appearance, etc. on page at runtime.

0
source

Styleid

Use the StyleId property to identify individual elements in the application for identification in ui testing and theme engines.

0
source

As IdoT says, if you define an object at runtime, you need to keep a reference to it in order to access it, since it is impossible to give a name. I use a dictionary like this:

 Dictionary<Guid, String> ControlsDictionary = new Dictionary<Guid, string>(); 

Then when I create the object:

 Button bt = new Button {Text="Yay!"}; bt.Clicked += OnButtonClicked; StackLayout.Children.Add(bt); ControlsDictionary.Add(bt.Id, "myname"); 

In the Click event handler, I can determine the "name" of the sender:

 async void OnButtonClicked(object sender, EventArgs args) { Button btn = (Button)sender; String NameofButton = ControlsDictionary[btn.Id]; } 

You can also get Id from the "name":

 Guid IdfromName = ControlsDictionary.First(x => x.Value == "Name").Key; 
0
source

Source: https://habr.com/ru/post/972353/


All Articles