Search code examples
formsimagexamarinxamarin.androidpopover

How to call method of another class from differnet class in Xamarin


I am new to xamarin.forms.

On main page I have button "Select Photos". When user click popup open with number of images. When user tap and image popup closes.

I want to do is when popup close; I want to display the selected image on main page so user know what image they selected.

So I have method on the popup page for image click. In the method I save the image name as variable. And I try to call another method which is on main page. The Method on the main page would get the variable and display the Image.

This is code when user tap image

        public void Idpty1(object sender, EventArgs args)
        {
            Signtype = "1";

            //save the image name as variable
            SelectedTypeImage = "idpty1.png";

            //On the Newphoto page; call close popup function. 
            new NewPhotoPage().ClosePopover();
}

This is function on main page and I am trying to call this function with above function.

 public void ClosePopover()
     {

       //Close the popover 
       PopupNavigation.Instance.PopAsync();

       //Get the variable which was set on the popover page (image name)
       SelectedTypeImage = MyPopupPage.SelectedTypeImage;

        // Source the image from variable. 
        SelectedType.Source = SelectedTypeImage ;


        //DisplayAlert("Alert2", SelectedTypeImage, "ok");

         System.Diagnostics.Debug.WriteLine("test");
    }

This is image code in the main page

<Image x:Name="SelectedType" Resources=""></Image>

In the above code; image part does not work, image source does not work also display alert does not work. BUT SYSTEM.DEBUG WORKS.

What I don't understand is function does get call but even display alert does not work.


Solution

  • I use something like

    public void Idpty1(object sender, EventArgs args)
            {
                Signtype = "1";
    
                //save the image name as variable
                SelectedTypeImage = "idpty1.png";
    
                //On the Newphoto page; call close popup function. 
               // new NewPhotoPage().ClosePopover();
    Xamarin.Forms.MessagingCenter.Send<App> ((App)Xamarin.Forms.Application.Current, "CallMethod");
    }
    

    in the constructor of Main Page.

    MessagingCenter.Subscribe(this, "CallMethod", (sender) => {
    // do something
    ClosePopover();  // <-- run u' method.
    });