Xamarin - 在导航后重新载入页面.PopModalAsync()
问题描述:
我使用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中进行了更改。这甚至有可能吗?
答
的正确方法在.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
}
}
要么通过在完成委托给你的模式,它关闭时执行,或使用MessagingCenter从发送消息模式到主页面 – Jason