locked
How to Update label text from another class? RRS feed

  • Question

  • User314515 posted

    I know its a silly question but I am new in Xamarin.forms I want to Update label text from another class in xamarin.forms. please guide me with a example. Thanks

    Wednesday, April 5, 2017 12:31 PM

Answers

  • User149784 posted

    Use the MessagingCenter to update Label.Text from another class: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 5:20 AM
  • User149784 posted
    public class MyClass1
    {
        public MyClass1()
        {
            MessagingCenter.Subscribe<MyClass2>(this, "MyCommandName", (sender, e) => { MyLabel.Text = e });
        }
    
        public void Dispose(bool disposing)
        {
            if (!disposing) return;
            MessagingCenter.Unubscribe<MyClass2>(this, "MyCommandName");
        }
    }
    
    public class MyClass2
    {
        private void OnMyCommand()
        {
            MessagingCenter.Send(this, "MyCommandName", "MyText");
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 4:55 PM

All replies

  • User310517 posted
     public static class Test
     {
            public static void Update(Label mylabel)
            {
                    mylabel.Text="My Text";
            }
     }
    

    Usage:

    var l=new Label(); Test.Update(l);

    Wednesday, April 5, 2017 3:49 PM
  • User169828 posted

    learn how to use Data Binding. Directly modifying a labels text property is NOT something you should be doing regularly.

    https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/databindingbasics/

    @AlexanderSheety that is terribly advice!!

    Wednesday, April 5, 2017 4:21 PM
  • User149784 posted

    Use the MessagingCenter to update Label.Text from another class: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 5:20 AM
  • User314515 posted

    Thank @Barbara I tried out example of messaging-center from here: https://github.com/xamarin/xamarin-forms-samples/tree/master/UsingMessagingCenter/UsingMessagingCenter

    but it is not working.

    Can you please show me an example of the messaging-center?

    Thursday, April 6, 2017 12:58 PM
  • User2148 posted

    for example I have something like this

    OnAppearing

                MessagingCenter.Subscribe<PagePopupListViewNumericoViewModel, int>(this, "ColliDaVentilare", (sender, arg) => {
                    ColliDaVentilare = arg;
                });
    

    OnDisappearing

                MessagingCenter.Unsubscribe<PagePopupListViewNumericoViewModel, int>(this, "ColliDaVentilare");
    

    Then I send a Message

                MessagingCenter.Send<PagePopupListViewNumericoViewModel, int>(this, "ColliDaVentilare", SelectedItem.Valore);
    
    Thursday, April 6, 2017 1:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Thursday, April 6, 2017 2:24 PM
  • User149784 posted
    public class MyClass1
    {
        public MyClass1()
        {
            MessagingCenter.Subscribe<MyClass2>(this, "MyCommandName", (sender, e) => { MyLabel.Text = e });
        }
    
        public void Dispose(bool disposing)
        {
            if (!disposing) return;
            MessagingCenter.Unubscribe<MyClass2>(this, "MyCommandName");
        }
    }
    
    public class MyClass2
    {
        private void OnMyCommand()
        {
            MessagingCenter.Send(this, "MyCommandName", "MyText");
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 6, 2017 4:55 PM
  • User314515 posted

    Thank you so much @Barbara. You made my day :)

    Friday, April 7, 2017 5:53 AM
  • User314515 posted

    Thanks guys for you quick response. I am having problem in remove polylines from the map. Can you please help me there here is the link of post

    https://forums.xamarin.com/discussion/comment/264008#Comment_264008

    Friday, April 7, 2017 5:56 AM
  • User149784 posted

    @faisalazeem20: You are welcome. :)

    Friday, April 7, 2017 7:03 AM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code:

    using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code:

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code:

    using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code:

    using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code:

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code:

    using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code:

    using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code:

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code:

    using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code:

    using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code:

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code:

    using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    Moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version:

    1. write your class so that it notifies of its properties changing
    2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code:

    using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: Numbered list:

    1. apples
    2. oranges
      1. write your class so that it notifies of its properties changing
      2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    See the attached text file for code example.

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Posting comments does not work for me?

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    test

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    class code: using System; using System.Collections.Generic; using System.ComponentModel; using Xamarin.Forms; namespace ClassBindingExample { public class MyClass : INotifyPropertyChanged { protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; private string labelText; public string LabelText { get { return labelText; } set { labelText = value; OnPropertyChanged("LabelText"); } } } }

    XAML code: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ClassBindingExample.MainPage"> <ContentPage.Content> <StackLayout Orientation="Vertical"> <Label x:Name="BindedLabel" FontSize="Medium"/> </StackLayout> </ContentPage.Content> </ContentPage>

    XAML.cs code: using System; using System.Diagnostics; using Xamarin.Forms; namespace ClassBindingExample { public partial class MainPage : ContentPage { private static MyClass _myClass = new MyClass(); public MainPage() { InitializeComponent(); BindingContext = _myClass; _myClass.LabelText = "this text is the value of MyClass.LabelText property" BindedLabel.SetBinding(Label.TextProperty, "LabelText", BindingMode.TwoWay); } } }

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User310368 posted

    Here's the short version: 1. write your class so that it notifies of its properties changing 2. bind the text property of the label to the property of your class

    See the attached text file for code example.

    Hope this helps!

    moto

    Tuesday, April 11, 2017 8:05 PM
  • User31568 posted

    I tried below but not working:

    public App()
    {
        InitializeComponent();
    
        MessagingCenter.Subscribe<App>(this, "lblHeader", (sender, e) => { lblHeader.Text = e });
    
        MainPage = new Startup();
    }
    

    Note that the lblHeader is in my App.xaml

    Wednesday, October 25, 2017 12:13 AM
  • User368729 posted

    the short version you say.....

    Friday, June 1, 2018 8:27 AM
  • User343509 posted

    Thanks @Barbara it's work for me :)

    Thursday, July 5, 2018 11:04 AM
  • User394157 posted

    Does any one a demo of the code, as I have been trying to get it to work and still have problems calling the function to change the label text from different functions, I have setup the public function in the app.cs so it can be used anywhere but still have problems.

    BindingContext = App.myClassobj; App.myClassobj.LabelText = "Some new test here";

    Friday, May 22, 2020 7:36 AM