2017-07-19 159 views
0

我使用Xamarin爲iOS和Android創建應用程序,我有一個用例,我按下一個按鈕,它將打開一個Modal,然後選擇一個按鈕後模式,它會更新一些信息,然後我需要彈出模態並重新加載它下面的主頁面。這怎麼可能?Xamarin - 在導航後重新載入頁面.PopModalAsync()

主頁:

public partial class MainPage : ContentPage 
{ 
    public MainPage(Object obj) 
    { 

     InitializeComponent(); 

     // do some things with obj... 

     BindingContext = this; 

    } 

    public ButtonPressed(object sender, EventArgs args) 
    { 
     Navigation.PushModalAsync(new SecondaryPage(newObj)); 
    } 

} 

次頁面:

public partial class SecondaryPage : ContentPage 
{ 
    public SecondaryPage(Object newObj) 
    { 

     InitializeComponent(); 

     BindingContext = this; 

    } 

    public ButtonPressed(object sender, EventArgs args) 
    { 
     // Do something to change newObj 

     Navigation.PopModalAsync(); 
    } 

} 

打完PopModalAsync(),我需要能夠調用的MainPage()構造函數,但它傳遞了 「newObj」我在SecondaryPage中進行了更改。這甚至有可能嗎?

+0

要麼通過在完成委託給你的模式,它關閉時執行,或使用MessagingCenter從發送消息模式到主頁面 – Jason

回答

0

的正確方法在.NET中做到這一點是建立一個事件:

public partial class SecondaryPage : ContentPage 
{ 
    public SecondaryPage(Object newObj) 
    { 

     InitializeComponent(); 

     BindingContext = this; 

    } 

    // Add an event to notify when things are updated 
    public event EventHandler<EventArgs> OperationCompeleted; 

    public ButtonPressed(object sender, EventArgs args) 
    { 
     // Do something to change newObj 
     OperationCompleted?.Invoke(this, EventArgs.Empty); 
     Navigation.PopModalAsync(); 
    } 

} 

public partial class MainPage : ContentPage 
{ 
    public MainPage(Object obj) 
    { 

     InitializeComponent(); 

     // do some things with obj... 

     BindingContext = this; 

    } 

    public ButtonPressed(object sender, EventArgs args) 
    { 
     var secondaryPage = new SecondaryPage(newObj); 
     // Subscribe to the event when things are updated 
     secondaryPage.OperationCompeleted += SecondaryPage_OperationCompleted; 
     Navigation.PushModalAsync(secondaryPage); 
    } 

    private void SecondaryPage_OperationCompleted(object sender, EventArgs e) 
    { 
     // Unsubscribe to the event to prevent memory leak 
     (sender as SecondaryPage).OperationCompeleted -= SecondaryPage_OperationCompleted; 
     // Do something after change 
    } 
} 
相關問題