如何在 Xamarin 中从不同的 class 调用另一个 class 的方法

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

我是 xamarin.forms 的新手。

在主页上我有按钮 "Select Photos"。当用户单击带有图像数量的弹出窗口时。当用户点击并且图像弹出窗口关闭时。

我想做的是当弹窗关闭时;我想在主页上显示所选图像,以便用户知道他们选择了什么图像。

所以我在弹出页面上有点击图片的方法。在方法中,我将图像名称保存为变量。我尝试调用主页上的另一种方法。主页上的方法将获取变量并显示图像。

这是用户点击图片时的代码

        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();
}

这是主页上的函数,我正在尝试用上面的函数调用这个函数。

 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");
    }

这是首页的图片代码

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

在上面的代码中;图像部分不工作,图像源不工作也显示警报不工作。但是 SYSTEM.DEBUG 有效。

我不明白的是功能确实得到了调用,但即使显示警报也不起作用。

我用的是

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");
}

在主页面的构造函数中。

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

在您的 Newphoto 页面上,只需将 ClosePopover 方法调用为:

YOURCLASSNAME.ClosePopover();

假设它们在同一个命名空间中。 否则,使用 using 添加 class 的命名空间。 如果您需要更多说明,请告诉我。