【问题标题】:MVVMCross MvxDialogFragment Restore Issue - Does not have MvxFragmentPresentationAttributeMVVMCross MvxDialogFragment 恢复问题 - 没有 MvxFragmentPresentationAttribute
【发布时间】:2020-07-07 22:23:34
【问题描述】:

我已从 4.2.3 升级到最新版本的 MvvmCross (6.4.1)。我和使用 Xamarin Android 不是 Xamarin 表单

在启动对话框的视图中,我执行以下操作

  • 创建从 MvxDialogFragment 派生的对话框片段
  • 为其分配视图模型
  • 然后在片段上调用 ​​ShowView

但是,当我旋转设备时,它在 OnCreate 中失败并显示消息

Your fragment is not generic and it does not have MvxFragmentPresentationAttribute attribute set!

这在 4.2.3 中没有发生。我以这种方式创建对话框的原因是我希望它根据我需要此对话框的位置使用不同的视图模型。例如,我想在对话框中显示不同的数据列表,但格式相同。

这似乎只有在我们应用 MvxFragmentPresentationAttribute 时才有效,它需要在设计时而不是运行时定义视图模型的类型。

我有什么办法可以做到这一点

任何帮助将不胜感激

【问题讨论】:

  • 如果您只是将此属性添加到您的活动中,ScreenOrientation = ScreenOrientation.Portrait, 似乎可以解决问题

标签: xamarin dialog mvvmcross


【解决方案1】:

如果您需要在运行时以某种方式指定 ViewModel 类型,您可以代替使用 MvxFragmentPresentationAttribute 装饰类,而是让它实现 IMvxOverridePresentationAttribute 并将其返回到那里,并在其中显示适当的 ViewModel。

类似:

public class MyDialog : MvxDialogFragment, IMvxOverridePresentationAttribute
{
    public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
    {
        return new MvxFragmentPresentationAttribute
        {
            ActivityHostViewModelType = myDynamicType
        };
    }
}

您在哪里实现某种逻辑以在某处获取myDynamicType

但是,您应该可以改用MvxDialogFragmentPresentationAttribute,如果您提供null 引用作为ActivityHostViewModelType,演示者将尝试使用最顶层的Android Activity 来演示它。

【讨论】:

  • 嗨@Cheesebaron,感谢您的回复。就我而言,它不是 ActivityHostViewModelType,而是对话框的 ViewModelType。当我添加属性时,我设置了 ViewModelType,然后它失败了,因为它无法创建我指定类型的非单例视图模型类型。是否/有任何关于手动创建对话框而不是使用导航服务的参考/文章。我的意思是 var mycustomFragment = new MyCustomDialogFragment(); myCustomDialogFragment.ViewModel = new MyCustomDialogFragmentViewModel(); myCustomDialogFragment.Show(fragmentManager, fragmentTag)
  • 请在使用 MvxDialogFragmentPresentationAttribute 时发布完整的错误信息
  • 问题是当我在 MvxDialogFragmentPresentation 属性中设置 ViewModelType 时,由于需要将参数传递给构造函数,因此无法构造该视图模型。我想我需要重新考虑我的视图模型以避免传递参数。我可能应该通过将该数据作为参数传递给片段来在片段中进行设置。
  • 在我的应用程序中有几种情况,我不使用导航服务来创建视图和视图对话框。我应该如何创建它们以使它们表现得好像它们是通过导航服务创建的一样。例如,我有从视图创建的对话框
猜你喜欢
  • 2021-12-08
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 2013-09-24
  • 1970-01-01
相关资源
最近更新 更多