有没有人得到Xamarin-Sidebar,MVVMCross&故事板一起工作?

时间:2014-12-23 03:33:57

标签: menu xamarin storyboard mvvmcross flyout

我已经尝试过我可以在网上找到的所有排列,但似乎无法让它发挥作用。

我在故事板中定义了我的iOS应用程序UI,MVVM框架主要是MVVMCross,其中引入了一些ReactiveUI。

我定义了一个RootViewController,我生成了附加到AppDelegate的SideBarController,如下所示:

[Register ("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate
{
    public override UIWindow Window { 
        get;
        set;
    }

    public AppDelegate ()
    {
    }

    public SidebarController SidebarController { get; set; }
    public override void FinishedLaunching (UIApplication application)
    {
        var presenter = new MvxModalSupportTouchViewPresenter (this, Window);
        var setup = new Setup (this, presenter);

        setup.Initialize ();

        var startUp = Mvx.Resolve<IMvxAppStart> ();
        startUp.Start ();
    }
}

在RootViewController中我有:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (ViewModel == null)
            return;

        var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;      
        appDelegate.SidebarController = new SidebarController (this, 
            (UIViewController)this.CreateViewControllerFor<LoginViewModel>(), 
            (UIViewController)this.CreateViewControllerFor<SideMenuViewModel>());
        appDelegate.SidebarController.MenuWidth = 250;
        appDelegate.SidebarController.ReopenOnRotate = false;
        appDelegate.SidebarController.MenuLocation = SidebarController.MenuLocations.Right;
    }

用户成功登录后,会导航到其上有汉堡按钮的登录页面。一旦用户点击汉堡按钮,侧面菜单就不会显示,但ToggleMenu()调用之前和之后的所有跟踪都会被执行。我试图让这个工作起作用,但3天之后我想我可能会给自己一个脑震荡。

有没有人试图让这个组合工作?

2 个答案:

答案 0 :(得分:3)

对我而言,这是有效的。我不确定你的问题在哪里,但这是我的代码:

[Register ("AppDelegate")]
    public partial class AppDelegate : MvxApplicationDelegate
    {
        UIWindow window;

        public RootViewController RootViewController { get { return window.RootViewController as RootViewController; } }

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow(UIScreen.MainScreen.Bounds);

            var presenter = new Presenter(this, window);

            var setup = new Setup(this, presenter);
            setup.Initialize();

            var startup = Mvx.Resolve<IMvxAppStart>();
            startup.Start();

            window.MakeKeyAndVisible();

            return true;
        }
    }

对于rootcontroller:

public class RootViewController: UIViewController, IMvxCanCreateTouchView
    {
        public SidebarController SidebarController { get; private set; }
        private UIViewController root;

        public RootViewController (UIViewController root)
        {
            this.root = root;
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            var menuContentView = this.CreateViewControllerFor<MenuViewModel> () as MenuViewController;
            SidebarController = new SidebarController (this, root, menuContentView) {
                MenuLocation = SidebarController.MenuLocations.Left,
                HasShadowing = false
            };
        }

        public void NavigateToView (UIViewController view)
        {
            SidebarController.ChangeContentView (new UINavigationController (view));
        }
    }

作为我的其他控制器的基类我有这个:

public interface IControllerWithCustomNavigation
    {
        void NavigateToView (UIViewController controller);
    }

public class BaseController<TViewModel>: MvxViewController where TViewModel: BaseViewModel
    {
        protected bool NavigationBarEnabled = true;

        public new TViewModel ViewModel {
            get { return (TViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }

        public BaseController (string nib) : base (nib, null)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            if (NavigationBarEnabled) {
                NavigationController.NavigationBarHidden = false;

                updateNavigationBar ();

                if (isRootViewModel()) {
                    addNavigationBarItemForMenu ();
                }
            } else if (NavigationController != null) {
                NavigationController.NavigationBarHidden = true;
            }
        }

        private void updateNavigationBar()
        {
            var navigationBar = NavigationController.NavigationBar;

            var colorTint = UIColor.FromRGB (133, 231, 110);
            var colorWhite = UIColor.White;

            navigationBar.BarTintColor = colorWhite;
            navigationBar.TintColor = colorTint;
            navigationBar.SetTitleTextAttributes (new UITextAttributes {
                TextColor = UIColor.FromRGB (13, 19, 26)
            });
        }

        private bool isRootViewModel()
        {
            return NavigationController.ViewControllers.Length < 2; // Every view, even the RootView, is counted here.
        }

    private void addNavigationBarItemForMenu()
    {
        var sidebarButton = new UIBarButtonItem (
            UIImage.FromFile ("menu_icon.png"),
            UIBarButtonItemStyle.Plain,
            (object sender, EventArgs e) => {
                (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.SidebarController.ToggleMenu();
            }
        );

        NavigationItem.SetLeftBarButtonItem (sidebarButton, true);
    }

    public virtual void NavigateToView (UIViewController controller)
    {
        (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.NavigateToView (controller);
    }
}

答案 1 :(得分:3)

因此,在查看了Martijn发布的代码,然后通过他之前的帖子实际进行了一些窥探之后,我决定最好创建自己的MvxViewPresenter,到目前为止它的工作原理。

public class SideBarControllerTouchViewPresenter : MvxModalSupportTouchViewPresenter
{
    private UIWindow _window;

    public SidebarController SidebarController {
        get ;
        set ;
    }

    public UIViewController RootController {
        get;
        private set;
    }

    public SideBarControllerTouchViewPresenter (UIApplicationDelegate applicationDelegate, UIWindow window) 
        : base(applicationDelegate, window)
    {
        _window = window;
    }

    public override void ChangePresentation (Cirrious.MvvmCross.ViewModels.MvxPresentationHint hint)
    {
        base.ChangePresentation (hint);
    }

    protected override void ShowFirstView (UIViewController viewController)
    {
        base.ShowFirstView (viewController);
    }

    protected override UINavigationController CreateNavigationController (UIViewController viewController)
    {

        var navController = new UINavigationController (viewController);
        var menuController = UIStoryboard.FromName ("storyboard", null).InstantiateViewController ("MenuTableViewController") as MenuTableViewController;
        RootController = new UIViewController ();
        SidebarController = new SidebarController (this.RootController, navController, menuController);
        return navController;
    }

    protected override void SetWindowRootViewController (UIViewController controller)
    {
        _window.AddSubview (RootController.View);
        _window.RootViewController = RootController;
    }
}

非常感谢让我在那里休息。