c# - FreshMVVM 并在弹出 Modal 之前重置 VM

标签 c# xamarin xamarin.forms freshmvvm

  • 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你可以看到它处理两个进程

  1. 发出 PageWasPopped 信号
  2. 调用导航服务以将页面从导航堆栈中弹出

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/

相关文章:

通过 OLEDB Jet 4.0 (dBase IV) 连接数据库的 Java JNI 调用 dll 与 Borland Database Engine (BDE) 在 JRE 7 环境中崩溃

c# - C# .NET 中的 PLC 编程

c# - 禁用后退按钮 C# Android Xamarin 代码没有响应

android - Realm 数据库是否可移植

android - 在MonoDroid中设置textview的文本时,“jobject”不得为IntPtr.Zero

c# - 在 iOS 上引发的 Xamarin.Forms.WebView.Navigating 事件用于内部导航

c# - 我的函数使用多少字节? (C#)

javascript - 绑定(bind)多个动态 jqgrid 时覆盖参数

xamarin - Prism 和控制模板

c# - 在 Xamarin.Forms 中创建默认短信应用程序