2017-06-20 22 views
0

我给用户一个页面来编辑listview项目的内容。是否可以转到其他页面,等到用户返回到原始页面,然后继续该功能?

public async void OnEdit(object sender, EventArgs e) 
{ 
    var menuItem = ((MenuItem)sender); 

    if (menuItem != null) 
    { 
     var selectedZone = (ViewModels.ZoneViewModel)menuItem.CommandParameter; 

     // Send to edit page with selectedzones' contents. 
     await Navigation.PushAsync(new ZonePage(selectedZone.Name, selectedZone.Address, selectedZone.IdentitySource, selectedZone.Username, selectedZone.Password)); 

     //Wait until user returns from page 

     //Edit logic here 
    }  
} 

这是将用户带到那里的说明。因此,在将用户发送到其他页面之后,我想等待他在编辑页面上完成编辑,然后返回以完成该功能。

我打算以不同的方式做到这一点,但它并没有变成我想要的样子。我需要var menuItem = ((MenuItem)sender);从列表中获取所选项目,并且不知道在我的情况下使用此项工作的另一种方法。

这可能吗?

回答

1

您可以尝试MessagingCenter在两个页面之间进行通信。 Xamarin.Forms MessagingCenter允许视图模型和其他组件进行通信,而不必知道除了简单消息协议以外的任何其他组件。

要在消息中传递参数,请在Subscribe泛型参数和Action特征中指定参数Type。

MessagingCenter.Subscribe<MainPage, string> (this, "Hi", (sender, arg) => { 
    // do something whenever the "Hi" message is sent 
    // using the 'arg' parameter which is a string 
}); 

要发送具有参数的消息,请在发送方法调用中包含Type泛型参数和参数值。

MessagingCenter.Send<MainPage, string> (this, "Hi", "John"); 

Here is the more detailed official documentation with examples.

+0

谢谢您的答复,我得到了这个工作,但现在我有一个问题,我想。我需要发送4个字符串和1个int,而不是只有一个字符串。我可以用一条消息而不是5条来做到这一点吗?或者这是不可能的? –

+0

根据文档可以传递任何C#对象。所以我假设你也可以传递一个对象列表。查看文档以获取更多信息。 –

相关问题