简体   繁体   English

将数据从平台特定页面传递到Xamarin.Forms中的PCL

[英]Passing data from platform specific page to PCL in Xamarin.Forms

I've created a platform specific page using PageRenderer and I need to pass data back from this page when a button is clicked. 我已经使用PageRenderer创建了特定于平台的页面,单击按钮时需要从该页面传回数据。

I've looked at using the DependancyService but all I can find is to call a method in the platform project from the PCL, I need to do the opposite, is this possible? 我已经看过使用DependancyService,但我能找到的就是从PCL调用平台项目中的方法,我需要做相反的事情,这可能吗? Or would I be better advised to look at using traditional Event - EventHandler, is this the preferred approach in Xamarin or is there a better way to handle this in Xamarim.Forms? 还是建议我使用传统的Event-EventHandler,这是Xamarin中的首选方法,还是在Xamarim.Forms中有更好的方法来处理?

The data is need to pass back is an object; 数据需要传回的是一个对象;

public class CallAcknowledged
{
    public string CustomerName { get; set; }
    public DateTime CallAcknowledged { get; set; }
    public byte[] Signature { get; set; }
}

I've managed to find the answer in this post from @AlessandroCaliaro on the Xamarin Forum https://forums.xamarin.com/discussion/70235/xamarin-forms-messagingcenter-between-two-differents-pages-projects 我已经设法在Xamarin论坛上的@AlessandroCaliaro的这篇文章中找到答案了https://forums.xamarin.com/discussion/70235/xamarin-forms-messagingcenter-between-two-differents-pages-projects

I needed to do use the PCLProjectName.App as the source of the message and it works a dream. 我需要使用PCLProjectName.App作为消息的来源,并且它可以实现梦想。

Sender 发件人

MessagingCenter.Send<MyMobileApp.App, string>((MyMobileApp.App)Xamarin.Forms.Application.Current, "Acknowledged", fullNameValue);

Subscriber 订户

MessagingCenter.Subscribe<MyMobileApp.App, string>((App)Application.Current, "Acknowledged", (sender, arg) =>
        {
            Debug.WriteLine("Full Name : " + arg);
        });

Thanks to @MarkusMichel for pointing me in the right direction 感谢@MarkusMichel为我指出正确的方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM