Update label from one Page to another in Xamarin Forms

wilmer chacon 0 Reputation points
2023-08-29T12:46:19.7433333+00:00

Hello, I hope you are well... I am starting in Xamarin... I have something to know... I have two pages on page 1 I have a label, from this page I go to another to look for a name on page 2, I want to update the Text when I choose this name of that Label of that page1… Thank you

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,337 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,081 Reputation points Microsoft Vendor
    2023-08-30T03:05:05.5866667+00:00

    Hello,

    For the problem that some data of one page needs to respond to data changes of another page, it is recommended that you use the singleton ViewModel to bind the data of the two pages.

    You can refer to the steps for the solution in Collection view mirror to list view and the following sample code.

    For the ViewModel:

    public class CommonViewModel : INotifyPropertyChanged
    {
        private static CommonViewModel instance = null;
        // This is a simple singleton pattern
        public static CommonViewModel Instance
        {
           get
            {
                if (instance == null)
                {
                    instance = new CommonViewModel();
                }
                return instance;
            }
        }
        private CommonViewModel()
        {
            Names = new ObservableCollection<string>();
            Label_Text = "test";
            Names.Add("a");
            Names.Add("b");
            Names.Add("c");
            Names.Add("d");
            Names.Add("e");
        }
    
        private string selectedItem;
       public string SelectedItem
        {
            get { return selectedItem; }
            set
            {
                if (selectedItem != value)
                {
                    selectedItem = value;
                   //After selecting the string in Picker, update it to Label.
                    Label_Text = selectedItem;
                    OnPropertyChanged();
                }
            }
        }
    
       public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "") =>
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    
       private string label_text;
       public string Label_Text
        {
            get { return label_text; }
            set
            {
                if (label_text != value)
                {
                    label_text = value;
                    OnPropertyChanged();
                }
            }
        }
    
       private ObservableCollection<string> names;
        public ObservableCollection<string> Names
        {
            get { return names; }
            set
            {
                if (names != value)
                {
                    names = value;
                    OnPropertyChanged();
                }
            }
        }
    }
    

    For Page2, I used a Picker to complete the selection of data.

    <Picker x:Name="picker" ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedItem}"
    Title="Select a monkey"
    TitleColor="Red"></Picker>
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.