2017-06-20 21 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#對象。所以我假設你也可以傳遞一個對象列表。查看文檔以獲取更多信息。 –

相關問題