在MonoTouch上集成第三方控制器和MVVMCross

时间:2013-08-09 19:11:30

标签: xamarin.ios mvvmcross

我想使用已经从UIViewController(https://bitbucket.org/thedillonb/monotouch.slideoutnavigation/src/f4e51488598b/MonoTouch.SlideoutNavigation?at=master)继承的第三方视图控制器,我如何将其与MVVMCross集成?

我可以直接获取源代码并将其更改为继承自MvxViewController,但我猜测我会在其他库中遇到此问题。

我需要实现MvxViewController的所有接口吗? IMvxTouchView? IMvxEventSourceViewController?

2 个答案:

答案 0 :(得分:1)

您可以使用下面的自定义视图展示器,这几乎是使用SlideOutNavigation从我的应用程序中直接显示的。

public class Presenter
    : IMvxTouchViewPresenter
{
    private readonly MvxApplicationDelegate applicationDelegate;
    private readonly UIWindow window;
    private SlideoutNavigationController slideNavigationController;
    private IMvxTouchViewCreator viewCreator;

    public Presenter(MvxApplicationDelegate applicationDelegate, UIWindow window)
    {
        this.applicationDelegate = applicationDelegate;
        this.window = window;

        this.slideNavigationController = new SlideoutNavigationController();

        this.slideNavigationController.SlideWidth = 200f;

        this.window.RootViewController = this.slideNavigationController;

    }

    public async void Show(MvxViewModelRequest request)
    {
        var creator = Mvx.Resolve<IMvxTouchViewCreator>();

        if (this.slideNavigationController.MenuView == null)
        {
            // TODO: MAke this not be sucky
            this.slideNavigationController.MenuView = (MenuView)creator.CreateView(new MenuViewModel());
            ((MenuView) this.slideNavigationController.MenuView).MenuItemSelectedAction = this.MenuItemSelected;
        }

        var view = creator.CreateView(request);


        this.slideNavigationController.TopView = (UIViewController)view;


    }

    public void ChangePresentation(MvxPresentationHint hint)
    {
        Console.WriteLine("Change Presentation Requested");
    }

    public bool PresentModalViewController(UIViewController controller, bool animated)
    {
        Console.WriteLine("Present View Controller Requested");
        return true;
    }

    public void NativeModalViewControllerDisappearedOnItsOwn()
    {
        Console.WriteLine("NativeModalViewControllerDisappearedOnItsOwn");
    }

    private void MenuItemSelected(string targetType, string objectId)
    {
        var type = Type.GetType(string.Format("App.Core.ViewModels.{0}ViewModel, AppCore", targetType));
        var parameters = new Dictionary<string, string>();
        parameters.Add("objectId", objectId);
        this.Show(new MvxViewModelRequest { ViewModelType = type, ParameterValues = parameters });
    }
}

答案 1 :(得分:1)

对于这种特殊情况,您实际上并不想进行任何数据绑定,因此您只需使用自定义演示者 - 例如请参阅@Blounty的回答,或查看此项目演示 - https://github.com/fcaico/MvxSlidingPanels.Touch


如果您确实需要转换第三方ViewController基类以便它们支持数据绑定,那么最简单的方法正是您所猜测的:

  • 继承他们以提供EventSource - ViewController
  • 继承自EventSource - ViewController以添加Mvx BindingContext

这项技术正是MvvmCross本身如何扩展UIViewControllerUITableViewControllerUITabBarController等每个人,以便提供数据绑定。

例如,请参阅:

请注意,因为C#没有任何Multiple-Inhertiance或任何真正的Mixin支持,ViewControllers的这种改编确实涉及一些剪切和粘贴,但我们试图通过使用事件挂钩和扩展来最小化这一点方法

如果它有帮助,那么在Integrating Google Mobile Analytics with MVVMCross中讨论了以前MvvmCross版本的iOS技术(显然现在这已经过时了 - 但是一般原则保持不变 - 我们通过继承来调整现有的viewcontroller)

在Android中,活动基类也遵循类似的过程 - 请参阅ActionBarSherlock with latest MVVMCross