Update MasterPage label text from ContentPage

atarekatarek Member ✭✭
edited February 23 in Xamarin.Forms

I have a "MasterPage" contains a label "contactName"

        <ListView.Header>
            <StackLayout Orientation="Horizontal" HeightRequest="150" Spacing="15" Padding="25,0,0,0" >
                <Label x:Name="contactName" Text="Helloo" LineBreakMode="WordWrap" FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center"/>
            </StackLayout>
        </ListView.Header>iew.ItemTemplate>
    </ListView>
</StackLayout>

And a "MainPage"

  public MainPage()
        {
            InitializeComponent();

            masterPage.listView.ItemSelected += OnItemSelected;

            if (Device.RuntimePlatform == Device.UWP)
            {
                MasterBehavior = MasterBehavior.Popover;
            }
        }

        void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MasterPageItem;
            if (item != null)
            {
                Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                masterPage.listView.SelectedItem = null;
                IsPresented = false;
            }
        }

And finally my "ContactsPage"

public class ContactsPageCS : ContentPage
{
    public ContactsPageCS ()
    {
        Title = "Contacts Page";
        Content = new StackLayout { 
            Children = {
                new Label {
                    Text = "Contacts data goes here",
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.CenterAndExpand
                }
            }
        };
    }

    private async void ProfilePickerSelectedIndexChanged(object sender, EventArgs e)
    {
        if (clinicPicker.SelectedItem == null)
        {
            return;
        }
        //Change Value of contactName label in master page
    }
}

What i want to do is change text of label "contactName" in "MasterPage" based on any event in "ContactsPage" "ex: dropdown index change"
I'm able to set value for label in MasterPage initialization and can update that value when "OnItemSelected" event is fires in "MainPage"

Attached image: I want to change label with text hello from "ContactsPage" based on dropdown event.

Any advice.
Regards,
Ahmed

Best Answer

Answers

Sign In or Register to comment.