简体   繁体   English

有没有一种很好的方法来模拟iOS中的Android片段模式?

[英]Is there a nice way to simulate Android's Fragment Pattern in iOS?

We're using MvvmCross in our apps. 我们在我们的应用程序中使用MvvmCross。 In our Android app, we use the NavigationDrawer for our Menu. 在我们的Android应用中,我们使用NavigationDrawer作为菜单。 We load our HomeView which contains the NavigationDrawer and the ContentFrame . 我们加载包含NavigationDrawer和ContentFrame HomeView。

<android.support.v4.widget.DrawerLayout >

    <!-- The main content view -->
    <FrameLayout android:id="@+id/content_frame" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer" />

</android.support.v4.widget.DrawerLayout>

When the OnCreate() method is triggered, we load an AudioPlayerFragment into the content_frame based on a database query. 触发OnCreate()方法时,我们会根据数据库查询将AudioPlayerFragment加载到content_frame In that same method we setup a listener that waits for the user to click one of the ListItem's in the navigation drawer. 在同一方法中,我们设置了一个监听器,等待用户单击导航抽屉中的ListItem之一。

protected override void OnCreate(Bundle bundle) {
   // do stuff to build the nav drawer
    _topDrawerList = FindViewById<MvxListView>(Resource.Id.left_drawer);
    _topDrawerList.ItemClick += (sender, args) = > SelectItem(args.Position, null);

    if(bundle == null)
        SelectItem(0, null);
}

private void SelectItem(int position, UserViewModel currentUser) {
    SupportFragmentManager.BeginTransaction()...
}

Our app is working great, and I like how the HomeView is the only View for the app. 我们的应用程序运行良好,我喜欢HomeView是应用程序的唯一视图。 Everything else is a Fragment. 其他一切都是碎片。

In our Core library, we have a ViewModel for the HomeView, and we also have a ViewModel for each Fragment in the app. 在我们的Core库中,我们有一个ViewView的ViewModel,我们还为应用程序中的每个Fragment提供了一个ViewModel。 Again, this is working well for us. 再次,这对我们来说很有用。

  • PCL PCL
    • ViewModels 的ViewModels
      • HomeViewModel HomeViewModel
      • AudioPlayerViewModel <-- vm for fragment AudioPlayerViewModel < - vm for fragment
      • LoginFragmentViewModel <-- vm for fragment LoginFragmentViewModel < - vm for fragment
  • Droid.Ui Droid.Ui
    • Fragments 片段
      • AudioPlayerFragment AudioPlayerFragment
      • LoginFragment LoginFragment
    • Views 查看
      • HomeView <-- Just a container View as the entry point to the app. HomeView < - 只是一个容器查看作为应用程序的入口点。 Everything else is fragmented... AudioPlayerFragment is loaded instantly. 其他一切都是碎片...立即加载AudioPlayerFragment。

Now I'm trying to build a matching iOS app, but I can't figure out how to structure it. 现在我正在尝试构建一个匹配的iOS应用程序,但我无法弄清楚如何构建它。 Essentially I'd like the exact same behavior where HomeView is the entry point and can contain both the SlidingPanel bits and some form of a ContentFrame that other "Views" can be loaded into. 本质上我喜欢完全相同的行为,其中HomeView是入口点,并且可以包含SlidingPanel位和某些形式的ContentFrame,其他“视图”可以加载到其中。

Unfortunately, right now I've got this stupid MenuView to deal with, and a corresponding MenuViewModel... and I really don't want either. 不幸的是,现在我已经有了这个愚蠢的MenuView来处理,还有一个相应的MenuViewModel ......我真的不想要。

  • PCL PCL
    • ViewModels 的ViewModels
      • HomeViewModel HomeViewModel
      • AudioPlayerViewModel <-- vm for fragment AudioPlayerViewModel < - vm for fragment
      • LoginFragmentViewModel <-- vm for fragment LoginFragmentViewModel < - vm for fragment
  • Touch UI 触摸UI
    • Views 查看
      • AudioPlayerView AudioPlayerView
      • HomeView HomeView
      • LoginView LoginView
      • MenuView <-- UGLY!!! MenuView < - UGLY !!!
    • ViewModels 的ViewModels
      • MenuViewModel <-- UGLY!!! MenuViewModel < - UGLY !!!

I really don't want to create a custom View/ViewModel for the Menu. 我真的不想为菜单创建自定义View / ViewModel。 I'm using SlidingPanels for my iOS "version" of the Navigation Drawer, and the tutorials I'm seeing are requiring an additional View/ViewModel for the menu. 我正在使用SlidingPanels作为导航抽屉的iOS“版本”,我看到的教程需要一个额外的View / ViewModel作为菜单。

Is there any way to do this and keep continuity with regards to ViewModels so that they can all be reused in a PCL? 有没有办法做到这一点并保持ViewModels的连续性,以便它们都可以在PCL中重用? Is there a nice (read Clean) way to build the layout structure in a similar way to Android? 是否有一种很好的(阅读清洁)方式以类似于Android的方式构建布局结构?

There have been some attempts to provide unified User Interfaces driven by Portable code. 已经有一些尝试提供由便携式代码驱动的统一用户界面。 Within MvvmCross, these have especially been driven by the MonoTouch.Dialog type approach to user interfaces. 在MvvmCross中,这些特别是由用户界面的MonoTouch.Dialog类型方法驱动。 One such attempt/experiment is called "auto-views" - you can see it in action in: 一个这样的尝试/实验称为“自动视图” - 您可以在以下操作中看到它:

Beneath this autoviews layer, further View code can be shared quite readily within MvvmCross at the Dialog level - eg see https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples 在此自动视图层下方,可以在Dialog级别的MvvmCross中轻松共享更多View代码 - 例如,请参阅https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples


However... with this said... most of the auto-views work to date has still focused on provide rapid prototyping rather than finished apps. 然而......据说...迄今为止,大多数自动视图工作仍然专注于提供快速原型而不是完成的应用程序。 For a full rich user interface, most developers still provide custom UIs. 对于完整的丰富用户界面,大多数开发人员仍然提供自定义UI。

I simply can't wrap my head around the iOS UI. 我根本无法绕过iOS UI。 It feels so very foreign to me in comparison to Android. 与Android相比,我感觉非常陌生。

This isn't that unusual - most XAML developers take to AXML faster than they do to UIKit. 这并不罕见 - 大多数XAML开发人员比UIKit更快地采用AXML。 However, if you take some time to build some UIView s and to build some user interfaces (pages) with UIViewController s, then I think you'll get the hang of UIKit quite quickly. 但是,如果您花一些时间构建一些UIView并使用UIViewController构建一些用户界面(页面),那么我认为您将很快得到UIKit的支持。 Further, I'm confident that once you start seeing the code-based power of things like UIKit animations then you'll even start to fall in love! 此外,我很有信心,一旦你开始看到像UIKit动画这样基于代码的力量,那么你甚至会开始坠入爱河!

You will be able to reuse your View Models, but not your Views. 您将能够重用View模型,但不能重用您的视图。 Today, PCL doesn't provide access to the UI stack, so you can't have portable views. 今天,PCL不提供对UI堆栈的访问,因此您无法拥有可移植视图。 For more details, have a look at the blog post How to Make Portable Class Libraries Work for You that our tester wrote. 有关更多详细信息, 请查看我们的测试人员撰写的博客文章如何使便携式类库为您工作

The approach I've settled on is to use the MvxViewController as my base entry point into the app. 我已经确定的方法是使用MvxViewController作为我的应用程序的基本入口点。

public class HomeView: MvxViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var myEntryFragement = new MyEntryFragment();
        Add(myEntryFragment);
    }
}

And then use MvxView 's as "Fragments" 然后使用MvxView作为“碎片”

public class MyEntryFragment: MvxView // essentially a UIView
{

    private MyEntryViewModel _viewModel;
    public MyEntryFragment()
    {
        // manually construct the _viewModel (or alternatively inject it in the constructor)
        // setup all your UI controls in here
    }

}

Then I just load and unload my "Fragment's" as needed. 然后我根据需要加载和卸载我的“片段”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM