How to Update label text from another class?

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

Tagged:

Best Answers

  • BarbaraLieskeBarbaraLieske USMember ✭✭
    edited April 6 Accepted Answer
    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");
        }
    }
    

Answers

  • AlexanderSheetyAlexanderSheety RUMember ✭✭
     public static class Test
     {
            public static void Update(Label mylabel)
            {
                    mylabel.Text="My Text";
            }
     }
    

    Usage:

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

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    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/data_binding_basics/

    @AlexanderSheety that is terribly advice!!

  • faisalazeem20faisalazeem20 PKMember

    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?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    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);
    
  • motomoto CAMember ✭✭

    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

  • BarbaraLieskeBarbaraLieske USMember ✭✭
    edited April 6 Accepted Answer
    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");
        }
    }
    
  • faisalazeem20faisalazeem20 PKMember

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

  • faisalazeem20faisalazeem20 PKMember
    edited April 7

    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

  • BarbaraLieskeBarbaraLieske USMember ✭✭

    @faisalazeem20: You are welcome. :)

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    Posting comments does not work for me?

  • motomoto CAMember ✭✭

    test

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • motomoto CAMember ✭✭

    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

  • JassimRahmaJassimRahma USMember ✭✭✭

    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

Sign In or Register to comment.