0

I want my label show the same word with a string.

The label in the .xaml file is like

...
<Label x:Name="Label_Test" Text="" />
...

And the string in the .cs file is like

...
string string_test = "";
...
...
protected async override void OnAppearing()
{
    string_test = "test";
...

For this example. The label should show "test" when entering this page.

How could I implement this binding?

Thank you!

CC.Wang
  • 111
  • 1
  • 12

1 Answers1

2

Here is a link to this question already answered: How to bind to a string

But let me help you understand some key concepts:

First, you have to make sure your string is a property like this:

  private string string_test;
  public string String_test
  {
      get { return string_test; }
      set { string_test = value; }
  }

Also, you will need to tell the xaml to update when the value of your string changes. This is done with the PropertyChangedEvent. You can see how this is done in the above link.

As far as the XAML goes, you can use X:Bind to link the label to the string in C#:

<Label Text="{x:Bind String_test}"/>

or

<Label Text="{Binding String_test}"/>