- FreshMVVM 3.0.0
- Xamarin 表单 4.2
我们的许多输入页面都是模态加载的,当用户按下“保存”时,我们会执行这样的命令
var newTemperature = new Temperature()
{
Date = DateTime.Now,
Value = this.TemperatureValue,
CaptureType = CaptureType.Manual,
IsModified = true,
};
await this.Services.DataService.SaveAsync(newTemperature);
// Save completed, now close modal.
await this.CoreMethods.PopPageModel(data, modal, animate);
如果你看CoreMethods.PopPageModel
call in GitHub你可以看到它处理两个进程
- 发出 PageWasPopped 信号
- 调用导航服务以将页面从导航堆栈中弹出
FreshMVVM
代码 handles the page being popped is in FreshPageModel 。除此之外,代码从 Appearing 和 Disappearing 事件中解脱出来,并将 BindingContext 设置为 null。从上面的顺序可以看出,这意味着 View
上的 BindingContext
在从堆栈弹出之前被设置为 null。
这样做的问题是,在 0.5 到 1.5 秒之间的短时间内,用户会看到一个看起来数据已全部重置的 View
。如果他们刚刚按下“保存”,这可能会非常令人不安。
如果我在调用 RaisePageWasPopped
之前颠倒 PopPageModel
中的逻辑顺序并从导航堆栈中弹出,此问题就会消失。
以前没有其他人遇到过这个问题吗?
任何 FreshMVVM
用户想要指出我建议的方法的错误吗?
最佳答案
我们对此问题的解决方案是实现我们自己的 PopPageModel 方法,该方法本质上会切换顺序,以便在调用 RaisePageWasPopped
之前在导航堆栈上调用 PopPage
当我们想要关闭页面时,这就是我们所说的
public Task DismissAsync(bool modal = true, bool animate = true)
{
return this.DispatcherService.RunOnUiThreadAsync(
async () =>
{
string navServiceName = this.CurrentNavigationServiceName;
if (this.IsModalFirstChild)
{
await this.CoreMethods.PopModalNavigationService(true);
}
else
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService>(navServiceName);
await rootNavigation.PopPage(modal, animate);
if (modal)
{
this.RaisePageWasPopped();
}
}
});
}
关于c# - FreshMVVM 并在弹出 Modal 之前重置 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60171744/