Home > OS >  How to pass value from my class file to xaml file?
How to pass value from my class file to xaml file?

Time:01-28

I'm learning Xamarin, and I'm trying to understand how can I pass a value from my class file to a .xmal file for display? Is that data binding or something else.

Example: My file.cs

namespace MyApp.Views
{
    public partial class LandingPage : ContentPage
    {
        public LandingPage()
        {
           string myvalue = "hello world";
        }
     }
}

file.xaml

<StackLayout>
  <Label Text="myvalue" />
</StackLayout>

I want the "myvalue" to pass from my class to my xaml file.

CodePudding user response:

yes, data binding is the answer

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

you can only bind to public properties

public string MyValue { get; set; }

public LandingPage()
{
   InitializeComponent();

   MyValue = "hello world";

   this.BindingContext = this;
}
  •  Tags:  
  • Related