xamarin.forms - 用 MvvmCross 实现视图模型的条件表示的方法是什么?
问题描述
我有一个登录视图模型(带有相应的页面)。在这种特殊情况下,我使用的是 Xamarin.Forms。
我需要的是作为导航堆栈的通用视图呈现的登录视图,作为可以用 [MvxModalPresentationAttribute] 注释的视图。
我在两种情况下展示了这种观点:
- 当应用程序启动且用户未登录时出现的第一个视图(以及在未登录的情况下继续工作);
- 在应用程序工作期间(如果用户未登录),但用户请求了某些功能,这需要先登录。
我想,Custom Presenter 是实现这一目标的方法,就像这样(对于 iOS,例如):
public class GeneralPresenter : MvxIosViewPresenter
{
public override void Show(MvxViewModelRequest request)
{
// ...
base.Show(request);
}
}
但是,我并没有完全遵循下一步应该采取的措施。(特别是,如果有任何关于 Xamarin.Forms 的特定内容,也应该完成)。
有什么提示吗?
解决方案
在 Mvvmcross.core 5.7.0 上,如果你想在 iOS 上呈现一个模态样式的视图,你可以MvxModalPresentation
给视图添加一个属性:
[MvxModalPresentation(
// Add this to modify the present view's style
//ModalPresentationStyle = UIModalPresentationStyle.PageSheet,
//ModalTransitionStyle = UIModalTransitionStyle.CoverVertical
)]
public class SecondView : MvxViewController
{
...
}
那么这个视图的呈现方式和push一样:
private readonly Lazy<IMvxNavigationService> _navigationService = new Lazy<IMvxNavigationService>(Mvx.Resolve<IMvxNavigationService>);
async private void ExecuteCommand()
{
await _navigationService.Value.Navigate<SecondViewModel>();
}
最后驳回这个观点应该是这样的:
async private void ExecuteCommand()
{
await _navigationService.Value.Close(this);
}
更新:
将 Mvvmcross 更新到 6.0.1.0 后,我们可以使用该IMvxOverridePresentationAttribute
接口来定义视图的呈现样式。使视图实现接口:
public class SecondView : MvxViewController<SecondViewModel>, IMvxOverridePresentationAttribute
{
...
public MvxBasePresentationAttribute PresentationAttribute(MvxViewModelRequest request)
{
var instanceRequest = request as MvxViewModelInstanceRequest;
SecondViewModel viewModel = instanceRequest?.ViewModelInstance as SecondViewModel;
if (viewModel.IsModalView)
{
return new MvxModalPresentationAttribute();
}
return new MvxChildPresentationAttribute();
}
...
}
IsModalView
在我的 ViewModel 中定义。当我们想要呈现一个视图时,使用它来修改样式:
public class SecondViewModel : MvxViewModel<bool>
{
...
public override void Prepare(bool parameter)
{
IsModalView = parameter;
}
public bool IsModalView { set; get; }
...
}
// The navigate method
await _navigationService.Value.Navigate<SecondViewModel, bool>(false);
推荐阅读
- javascript - 检查类是否是类的子类而不实例化
- c# - char8 和 uchar8 等效
- flutter - Flutter - 无法在 android 上安装 apk
- linkedin-api - 如何从 LinkedIn Voyager API 获取已读回执?
- c++ - 调用函数指针时 CUDA 内核崩溃
- ruby-on-rails - Rails 新手尝试将月份和年份从表单传递到 url
- visual-studio-code - 如何在 VSCode 中禁用风筝提示片段设置?
- php - 无法在 PHP Laravel 项目中显示 JPEG 图像
- node.js - 存储文件(pdf),因此只有经过身份验证的用户才能访问它们(使用 nextjs)
- javascript - 如何按名称查找频道 DiscordJS 12