简体   繁体   English

如何将数据从模态形式传输到主体? Xamarin.Forms

[英]How to transfer data from the modal form to the main? Xamarin.Forms

I need to transfer data from the modal page to the main page when I click on the button. 单击按钮时,我需要将数据从模式页面传输到主页。 But something does not work out. 但是有些东西行不通。

It is necessary that there should be wait after the line until the modal window is closed: 行后必须等待,直到关闭模态窗口:

await Navigation.PushModalAsync(modal);                         

MainPage: 主页:

string AncorName { get; set; }
Map map = new Map();

public MainPage() 
{
    AddCircle();
}

void AddCircle()
{
    map.MapClicked += async (sender, e) =>
    {
        Subscribe();
        var modal = new ModalPage();
        await Navigation.PushModalAsync(modal);

        var circle = CircleFactory.GetCircle(DataConfig.CENTER, AncorName); // AncorName = null
        map.Circles.Add(circle);

        circle.Clicked += (circleSender, ev) =>
        {
            var c = circleSender as Circle;
            DisplayAlert("Clicked on anchor", c.Tag as string, "Close");
        };
    };
}

void Subscribe() =>
    MessagingCenter.Subscribe<Page, string>(this, "AnchorsName", (sender, arg) =>
        AncorName = arg // data with modal page
    );

ModalPage: ModalPage:

void OnClicked(object sender, EventArgs)
{
    MessagingCenter.Send(this, "AnchorsName", ancorName.Text); // my data
    await Navigation.PopModalAsync();
}

Help me please. 请帮帮我。

I suggest to use MessagingCenter to send data between pages. 我建议使用MessagingCenter在页面之间发送数据。 Your modal "Send" data to your first page that "subscribe" a message. 您的模式“发送”数据到“订阅”消息的第一页。

Here you can find some info. 在这里您可以找到一些信息。

Otherwise, pass an Object to "Modal" constructor and modify it. 否则,将一个对象传递给“模态”构造函数并对其进行修改。 When you close the Modal page, in the Object you should find modified data. 关闭“模态”页面时,应在“对象”中找到已修改的数据。

You should Subscribe in OnAppearing and Unsubscribe in OnDisappearing. 您应该在OnAppearing中订阅,而在OnDisappearing中取消订阅。

MessagingCenter.Subscribe<ModalPage, string>(this, "AnchorsName", (sender, arg) =>
    AncorName = arg // data with modal page
);

You can try to change your Send with 您可以尝试更改发送方式

MessagingCenter.Send<ModalPage, string>(this, "AnchorsName", ancorName.Text); // my data

Did you try to get the log when the subscription method was called? 调用订阅方法时,您是否尝试获取日志? Are you sure it's not called? 您确定它没有被调用吗?

If it's called but what you expected did not happen. 如果它被调用,但是您期望的未发生。 Please try to use OnPropertyChanged() when AncorName's getter is called. 调用AncorName的getter时,请尝试使用OnPropertyChanged()。

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

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