Barney Chambers
Barney Chambers

Reputation: 2783

Change XAML of different Page Xamarin

I have two pages in my Xamarin Forms App, MainPage and Dashboard. I am looking to change the name of a label on my Dashboard page, when I change from my MainPage to my Dashboard. Changing page is achieved using this function on my MainPage.

String labelText = "Hello World"; 

public async void openPage(Page page)
    {
        await Navigation.PushAsync(page);

    }

How do I get my labelText to update a <Label/> which I have in myDashboard.xaml` page?

Upvotes: 0

Views: 749

Answers (2)

Renjith
Renjith

Reputation: 682

Use MVVM pattern as it is mostly preferred for xamarin.

In xaml for Dashboard page, assign binding for label text as follows:

In the view model declare the property as follows

private string _labelText="<default string to appear>";
public string LabelText
{
    get { return _labelText; }
    set
    {
        _labelText= value;
        OnPropertyChanged ();
    }
}

Now when you navigation to this Dashboard page, property for LabelText will be assigned with default value you give.

If you want to show value passed from page1 as label, pass the value on

pushAsync(Navigation.PushAsync(new Page2("<string to be passed>"))

and then assign the corresponding value to label in constructor/OnAppearing method/property directly.

Upvotes: 1

Dan Siegel
Dan Siegel

Reputation: 5799

Like is so often the case there is more than one way to accomplish your goal. To handle this using the base Xamarin Forms classes you should look at the MessagingCenter. That said to really develop out a Xamarin Forms app, I would suggest you use an MVVM framework like Prism which has been used for years with XAML markup.

Upvotes: 0

Related Questions