Get result of an int changed in a ContentPage

Viewed 41

I have an app with the following page structure:

MainPage
        ->SettingsPage
                      ->InputLanguagePage
                      ->OutputLanguagePage

All pages are ContentPages.

InputLanguagePage and OutputLanguagePage are the very same pages, so I don't want to program the very same page twice.

I can surely instantiate the same page twice, but I have no idea how I should get the return value.

I spent really long trying to describe my problem. Perhaps the code below demonstrates the problem much better.

As you can see, I seem to be unable to get the return value of the LanguageSelectionPage since it's shown asynchronous. Also changing it to Navigation.PushModalAsync() didn't change anything.

What would be the best way to have a ContentPage return a value or change a value and be notified about it?

Thank you.

MainPage:

public class MainPage : ContentPage
{
    private async void OnSettingsSelected(object sender, EventArgs e)
    {
        SettingsPage nSettings = new SettingsPage();
        await this.Navigation.PushAsync(nSettings);
    }
(...)

SettingsPage:

public class SettingsPage : ContentPage
{
    public SettingsPage()
    {
        protected override async void OnAppearing()
        {
            base.OnAppearing();

            _btnInputLanguage.Clicked += async (sender, e) =>
            {
                LanguageSelectionPage nInputLanguage = new LanguageSelectionPage();
                nInputLanguage.SelectedLCID = App.Settings.InputLanguageLCID;
                await Navigation.PushAsync(nInputLanguage);
                //The user can change "int SelectedLCID" within the ContentPage, but I have no idea how I could access it
                return;
            };

            _btnOutputLanguage.Clicked += async (sender, e) =>
            {
                LanguageSelectionPage nOutputLanguage = new LanguageSelectionPage();
                nOutputLanguage.SelectedLCID = App.Settings.OutputLanguageLCID;
                await Navigation.PushAsync(nOutputLanguage);
                //The user can change "int SelectedLCID" within the ContentPage, but I have no idea how I could access it
                return;
            };

(...)

LanguageSelectionPage:

public class LanguageSelectionPage : ContentPage
{
    public int SelectedLCID { get; set; }

    protected override async void OnAppearing()
    {
        base.OnAppearing();

(...)

2 Answers

I will give solutin to pass it ass parameter when yu ask to load page

public partial class InputLanguagePage : ContentPage
{
    public InputLanguagePage(string Selection)
    {
        InitializeComponent();
    }
}

public partial class OutputLanguagePage : ContentPage
{
    public OutputLanguagePage(string Selection)
    {
        InitializeComponent();
    }
}

In settingsPage Button Click

_btnInputLanguage.Clicked += async (sender, e) =>
{
   Navigation.PushAsync(new InputLanguagePage ("SelectLanguage"));
   return;
};

 _btnOutputLanguage.Clicked += async (sender, e) =>
{
    Navigation.PushAsync(new OutputLanguagePage("SelectLanguage"));
    return;
};

There are a lot of ways to solve this:

  1. pass a completion handler to the 2nd page
  2. raise an event on the 2nd page and subscribe from the 1st page
  3. pass an object whose value will be set by the 2nd page
  4. use Messaging

*

// on the first page, listen for a message
MessagingCenter.Subscribe<SecondPage, string> (this, "ValueSet", (sender, arg) => {
  // arg will contain the value passed by the sender
});

// when the value is set on the 2nd page, send a message
MessagingCenter.Send<SecondPage, string> (this, "ValueSet", someValue);
Related