【发布时间】:2018-07-06 11:48:03
【问题描述】:
我正在使用 MVVMCross (5.1.1) 为 iOS 和 Android 设计一个具有共享逻辑的跨平台应用程序。 在我的整个应用程序中,我在顶部有一个固定的工具栏,显示当前视图的标题以及一个按钮。在栏下方,界面正在从视图更改为视图
Android 部分:
在 Android 上,我创建了一个可重复使用的布局,我使用 include 将其嵌入到当前布局中。
在我的便携式项目中,我有一个 BaseViewModel,它具有可重复使用的工具栏布局绑定到的属性。所有其他 ViewModel 都派生自这个基类。这样我就可以在一个 ViewModel 中拥有显示屏幕的所有可绑定属性,而无需嵌套,但可以自己查看:
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_login" />
<RelativeLayout
android:id="@+id/parentLoginLayout"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MvxBind="Text Pin"
/>
</RelativeLayout>
</RelativeLayout>
toolbar_login.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_login"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minWidth="25px"
android:minHeight="25px">
<TextView
app:MvxBind="Click ToolbarMenuCommand"
/>
<!-- some other -->
</android.support.v7.widget.Toolbar>
ViewModels.cs
using System.Threading.Tasks;
using Mobile.Helpers;
using ViewModels.Base;
using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using Plugin.MessageBox;
namespace Mobile.ViewModels
{
public abstract class BaseViewModel : MvxViewModel
{
protected void NavigateToMainView()
{
NavigateTo<MainViewModel>();
}
private readonly IMvxNavigationService _navigationService;
protected BaseViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
}
public IMvxCommand ToolbarMenuCommand => new MvxCommand(OnMenuButtonClick);
protected abstract void OnMenuButtonClick();
}
public class LoginViewModel : BaseViewModel
{
private bool _menuVisibility;
private string _pin;
public LoginViewModel(IMvxNavigationService navigationService) : base(navigationService)
{
}
public bool MenuVisibility
{
get => _menuVisibility;
set => SetProperty(ref _menuVisibility, value);
}
public string Pin
{
get => _pin;
set => SetProperty(ref _pin, value);
}
protected override void OnMenuButtonClick()
{
MenuVisibility = !MenuVisibility;
}
}
}
iOS 部分:
我不完全确定如何在 iOS 上实现上述行为。我希望有人对我有一个好主意或一个好的示例项目,我可以看看。一般来说,重构 ViewModel 是没有问题的,因为我的想法在 iOS 上是不可能的。
关于 iOS 项目的一些事实:
- 我不使用故事板,但单个 .xib 是独立的 彼此之间
- 在我的 .xib 文件中,我使用自动布局约束来定位和 完全调整大小
我已经有了一些想法(现在无法测试):
1.想法:
- 使用上面的栏创建一个基础 .xib,约束以及 网点
- 根据之前创建的文件创建每个新的 xib 设计
这意味着我需要调整每个视图,以防我决定更改工具栏的某些内容,但到目前为止,我发现没有其他方法可以在没有两个不同 ViewController 的情况下将 .xib 嵌入另一个 .xib 中。我还读到继承会导致插座出现问题。
2。想法
- 每个 .xib 在顶部都有一个空视图,它充当容器 工具栏
- 有一个基础 ViewController,它从代码和构建工具栏 将其作为子项添加到容器视图中,并绑定属性 来自 BaseViewModel
在之前的 iOS 项目中,我注意到向布局添加视图会导致自动布局出现问题。可能也是一个不太好的解决方案?
3。想法
使用工具栏和下面的容器创建一个 xib,并将其用作母版页,这可能意味着拥有一个带有工具栏属性的 MasterViewModel 和一个嵌套的 ChildViewModel。 这可能是要走的路,但我不得不承认我不知道什么是最好的方法(对于 iOS 和 MVVMCross 来说还是很新的)。
有人对我有一些有用的提示吗?非常感谢!
【问题讨论】:
标签: android ios xamarin mvvmcross