Home > Enterprise >  Triger a function of Mainpage after closing a subform in Xamarin
Triger a function of Mainpage after closing a subform in Xamarin

Time:12-29

In my Xamarin Forms i have two Pages

  • MainPage
  • LoginPage

On Mainpage I have a function in cs file named as getfocus(). MainPage is Main Form and LoginPage is subform. LoginPage Appears on top of MainPage. once I close LoginPage I want getfocus() function of MainPage to be called. I tried "focused" property of contentpage in xaml but its not working. below is the code on LoginPage after which the function on mainpage should be called. Following line is used to close LoginPage.

await PopupNavigation.Instance.PopAsync(true);

CodePudding user response:

You can use MessagingCenter to achieve this function, according to your statement you seem to use the Popup Page Plugin plug-in.

I wrote a small example for your reference, hoping to solve your problem.

First in your await PopupNavigation.Instance.PopAsync(true); Add MessagingCenter.Send<object>(this, "Hi"); below the code; use this line of code to register MessagingCenter.

Then here is the MainPage code:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        MessagingCenter.Subscribe<object>(this, "Hi", (sender) =>
        {
            getfocus();
        });
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushPopupAsync(new LoginPage());
    }
    
    private  void getfocus()
    {

    }
}

Use MessagingCenter.Subscribe to receive and call the getfocus() method.

For more confidence about MessagingCenter, you can refer to:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

CodePudding user response:

unfortunately I have to change my navigation pattern. but thanx all for your support

  • Related