Is there a good way to simulate Android Fragment Pattern in iOS?

We use MvvmCross in our applications. In our Android app, we use NavigationDrawer for our menu. We load our HomeView, which contains NavigationDrawer and ContentFrame.

<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 method starts OnCreate(), we load the AudioPlayerFragment in content_framebased on the database query. In the same method, we configure a listener that expects the user to click one of the ListItem in the navigation drawer.

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 application works great, and I like how HomeView is the only view for the application. Everything else is a fragment.

ViewModel HomeView, ViewModel . , .

  • PCL
    • ViewModels
      • HomeViewModel
      • AudioPlayerViewModel < - vm
      • LoginFragmentViewModel < - vm
  • Droid.Ui
      • AudioPlayerFragment
      • LoginFragment
      • HomeView < - . ... AudioPlayerFragment .

iOS, , . , , HomeView SlidingPanel, ContentFrame, "".

, MenuView MenuViewModel... .

  • PCL
    • ViewModels
      • HomeViewModel
      • AudioPlayerViewModel < - vm
      • LoginFragmentViewModel < - vm
  • Touch UI
      • AudioPlayerView
      • HomeView
      • LoginView
      • MenuView < - UGLY!!!
    • ViewModels
      • MenuViewModel < - UGLY!!!

View/ViewModel . SlidingPanels "" iOS , , , /ViewModel .

ViewModels, PCL? () Android?

+4
3

, , MvxViewController .

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

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

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
    }

}

"" .

0

, . MvvmCross MonoTouch.Dialog . / "-" - :

autoviews MvvmCross Dialog - , . https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples


... ... , . - .

iOS. Android.

- XAML AXML, UIKit. , UIView () UIViewController s, , UIKit. , , , UIKit, !

+2

You can reuse your view models, but not your views. Today, PCL does not provide access to the user interface stack, so you cannot have portable views. For more information, see the Blog Post How to make portable class libraries for you , which our tester wrote about.

0
source

All Articles