如何从Xamarin中的differentnet类调用另一个类的方法

时间:2018-10-08 15:19:55

标签: forms image xamarin xamarin.android popover

我是xamarin.forms的新手。

在主页上,我具有“选择照片”按钮。当用户单击弹出窗口时,将打开图像数量。用户点击时,图像弹出窗口关闭。

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

因此,我在弹出页面上有用于单击图像的方法。在方法中,我将图像名称另存为变量。我尝试调用主页上的另一种方法。主页上的“方法”将获取变量并显示图像。

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

        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工作。

我不明白的是函数确实会被调用,但即使显示警报也不起作用。

2 个答案:

答案 0 :(得分:0)

我使用类似

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

答案 1 :(得分:0)

在您的Newphoto页面上,只需按以下方式调用ClosePopover方法:

YOURCLASSNAME.ClosePopover();

假设它们在同一名称空间中。 否则,使用using添加类的名称空间。 让我知道是否需要进一步说明。