2016-12-06 66 views
0

我試圖用這個componentexample以實現Xamarin.iOS一個側面菜單。 一切工作正常,除了我想關閉側面菜單的部分。Sidemenu不打烊 - Xamarin.iOS

現在它的工作原理是這樣的:我可以通過使用NavigationItem中的LeftButton或手指滑動來打開側邊菜單。但我無法用這兩種方法中的任何一種來關閉它。

有沒有人知道爲什麼會發生這種情況?我錯過了什麼?

另請參閱下面的代碼。

HomeView

public partial class HomeView : MvxViewController 
{ 
    public HomeView() : base("HomeView", null) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     // Perform any additional setup after loading the view, typically from a nib. 

     CGRect screenRect = UIScreen.MainScreen.Bounds; 
     NavigationController.View.Frame = new CGRect(0, 0, screenRect.Width, screenRect.Height); 

     var app = UIApplication.SharedApplication.Delegate as AppDelegate; 
     NavigationItem.SetLeftBarButtonItem(
      new UIBarButtonItem(UIImage.FromBundle("menu"), 
           UIBarButtonItemStyle.Plain, (sender, e) => 
           { 
            app.SidebarController.ToggleMenu(); 
           }), true); 
    } 

RootView

public partial class RootView : MvxViewController 
{ 
    public RootViewModel RootViewModel 
    { 
     get { return (RootViewModel)ViewModel; } 
     set { ViewModel = value; } 
    } 

    public RootView() : base("RootView", null) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     // Perform any additional setup after loading the view, typically from a nib. 

     if (ViewModel == null) 
      return; 

     var app = UIApplication.SharedApplication.Delegate as AppDelegate; 
     app.SidebarController = new SidebarController(this, 
                CreateViewFor(RootViewModel.Home, false), CreateViewFor(RootViewModel.Menu, true)); 
     app.SidebarController.MenuWidth = 220; 
     app.SidebarController.ReopenOnRotate = false; 
     app.SidebarController.MenuLocation = MenuLocations.Left; 
    } 

    public override void DidReceiveMemoryWarning() 
    { 
     base.DidReceiveMemoryWarning(); 
     // Release any cached data, images, etc that aren't in use. 
    } 

    private UIViewController CreateViewFor(IMvxViewModel viewModel, bool navBarHidden) 
    { 
     var controller = new UINavigationController(); 
     var screen = this.CreateViewControllerFor(viewModel) as UIViewController; 
     controller.PushViewController(screen, false); 
     controller.NavigationBarHidden = navBarHidden; 
     return controller; 
    } 
+0

我不知道你的代碼有什麼問題。但是我在我的項目中使用了側欄導航,它工作得很好。如果你想,我可以發佈我的代碼 – HeisenBerg

+0

你也用MVVMCross? –

+0

當我繼續調查這一點時,我發現RootViewController位於其他兩個控制器(Menu和Home ViewControler)的頂部,並且它阻止了這兩者之間的所有交互/事件。 –

回答

0

我設法從RootView.cs添加行下面的viewDidLoad中()來修復奇怪的行爲:

View.Frame = new CGRect(0, 0, 0, 0); 

出於某種原因,它看起來像從RootViewController的觀點是其他兩個以上,這是唯一的我設法使它工作。如果有人有更好的解決方案,請添加您的答案,我會嘗試。我所做的更像是一種解決方法。

@HeisenBerg有我們的項目之間的一個區別,我使用MVVMCross,這改變了一下多個屏幕之間的流動。但是謝謝你的幫助!

0

這是怎麼了,我實現了側欄導航在我的應用程序和它工作得很好。

RootViewController.cs

public class RootViewController : UIViewController 
{ 
    UIStoryboard _storyboard; 

    // the sidebar controller for the app 
    public SidebarController LeftSidebarController { get; private set; } 

    // the navigation controller 
    public NavigationController NavController { get; private set; } 

    // the storyboard 
    public override UIStoryboard Storyboard { 
     get { 
      if (_storyboard == null) 
       _storyboard = UIStoryboard.FromName ("Main", null); 
      return _storyboard; 
     } 
    } 

    public RootViewController() : base (null, null) 
    { 

    } 

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

     var programController = (CareProgramController)Storyboard.InstantiateViewController ("careProgramController"); // This is the menu.. 
     var leftMenuController = (LeftMenuController)Storyboard.InstantiateViewController ("leftMenuController"); // This is the initial ViewController 
     NavController = new NavigationController(); 
     NavController.PushViewController (programController, false); 
     LeftSidebarController = new SidebarController (this, NavController, leftMenuController) { 
      HasShadowing = false, 
      MenuWidth = 280, 
      MenuLocation = MenuLocations.Left 
     }; 
    } 
} 

BaseViewController.cs

public class BaseViewController : UIViewController 
{ 

    // provide access to the sidebar controller to all inheriting controllers 
    protected SidebarNavigation.SidebarController SidebarController { 
     get { 
      return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.LeftSidebarController; 
     } 
    } 

    // provide access to the navigation controller to all inheriting controllers 
    protected NavigationController NavController { 
     get { 
      return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.NavController; 
     } 
    } 

    // provide access to the storyboard to all inheriting controllers 
    public override UIStoryboard Storyboard { 
     get { 
      return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.Storyboard; 
     } 
    } 

    public BaseViewController (IntPtr handle) : base (handle) 
    { 
    } 


    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     SetupNavigationBar(); 
     SetDrawerToogle(); 
    } 

    void SetupNavigationBar() 
    { 
     if (NavController != null) { 
      NavController.NavigationBar.BarTintColor = AZConstants.PrimaryColor; 
      NavController.NavigationBar.Translucent = false; 
      NavController.NavigationBar.TintColor = UIColor.White; 
      NavController.NavigationBar.BarStyle = UIBarStyle.Black; 
     } 
    } 

    void SetDrawerToogle() 
    { 
     NavigationItem.SetLeftBarButtonItem (
      new UIBarButtonItem (UIImage.FromFile ("ic_menu_white.png").ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal) 
       , UIBarButtonItemStyle.Plain 
       , (sender, args) => { 
        SidebarController.ToggleMenu(); 
       }), true); 
    } 
} 

現在在我的應用程序的所有ViewControllers從BaseViewController延伸,這將有抽屜式導航

AppDelegate.cs

public class AppDelegate : UIApplicationDelegate 
{ 
    // class-level declarations 

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

    public override UIWindow Window 
    { 
     get; 
     set; 
    } 

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) 
    { 
     // Override point for customization after application launch. 
     // If not required for your application you can safely delete this method 
     UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, true); 

     GotoRootViewController(); 
     return true; 
    } 

    public void GotoRootViewController() 
    { 
     Window = new UIWindow(UIScreen.MainScreen.Bounds); 
     //Window.RootViewController.DismissViewController(true, null); 
     //If you have defined a root view controller, set it here: 
     Window.RootViewController = new RootViewController(); 

     // make the window visible 
     Window.MakeKeyAndVisible(); 
    }