Category / Section
How to handle events in SfNavigationDrawer of Xamarin.iOS application?
1 min read
Handling DidOpen and DidClose events in SfNavigationDrawer:
In latest 2017 Volume 2,we have removed Delegate API and SFNavigationDrawerDelegate class from SfNavigationDrawer control. Here we have explained how to hook the events using event handler instead of delegates.
The below codes explain how to hook the event using delegate class in our old implementation. And we have provided steps to move to event handler.
Code snippet:
namespace DelegateToEvent{ public partial class ViewController : UIViewController { protected ViewController(IntPtr handle) : base(handle) { // Note: this .ctor should not contain any initialization logic. } public override void ViewDidLoad() { base.ViewDidLoad(); SFNavigationDrawer navigationDrawer = new SFNavigationDrawer(); navigationDrawer.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); UIView mainContentView = new UIView(); mainContentView.Frame=new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); mainContentView.BackgroundColor = UIColor.FromRGBA(0, 0, 128, 0.2f); navigationDrawer.ContentView = mainContentView; UIView drawerContentView = new UIView(); drawerContentView.Frame = new CoreGraphics.CGRect(0, 0, 200, this.View.Frame.Height); drawerContentView.BackgroundColor = UIColor.FromRGBA(128, 0,0 , 0.8f); navigationDrawer.DrawerContentView = drawerContentView; navigationDrawer.DrawerHeaderHeight = 0; navigationDrawer.DrawerFooterHeight = 0; navigationDrawer.DrawerWidth = 200; navigationDrawer.Delegate = new NavigationDrawerDelegate(); this.View.AddSubview(navigationDrawer); } public override void DidReceiveMemoryWarning() { base.DidReceiveMemoryWarning(); // Release any cached data, images, etc that aren't in use. } } public class NavigationDrawerDelegate : SFNavigationDrawerDelegate { public override void DidClose(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen; } public override void DidOpen(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen;base.DidOpen(navigationDrawer); } }}
Step 1:Include the ValueChanged Event handler as follows.
namespace DelegateToEvent{ public partial class ViewController : UIViewController { protected ViewController(IntPtr handle) : base(handle) { // Note: this .ctor should not contain any initialization logic. } public override void ViewDidLoad() { base.ViewDidLoad(); SFNavigationDrawer navigationDrawer = new SFNavigationDrawer(); navigationDrawer.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); UIView mainContentView = new UIView(); mainContentView.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); mainContentView.BackgroundColor = UIColor.FromRGBA(0, 0, 128, 0.2f); navigationDrawer.ContentView = mainContentView; UIView drawerContentView = new UIView(); drawerContentView.Frame = new CoreGraphics.CGRect(0, 0, 200, this.View.Frame.Height); drawerContentView.BackgroundColor = UIColor.FromRGBA(128, 0, 0, 0.8f); navigationDrawer.DrawerContentView = drawerContentView; navigationDrawer.DrawerHeaderHeight = 0; navigationDrawer.DrawerFooterHeight = 0; navigationDrawer.DrawerWidth = 200; navigationDrawer.Delegate = new NavigationDrawerDelegate(); //NavigationDrawer DidOpen EventHandler navigationDrawer.DidOpen += (sender, e) => { }; //NavigationDrawer DidClose EventHandler navigationDrawer.DidClose += (sender, e) => { }; this.View.AddSubview(navigationDrawer); } public override void DidReceiveMemoryWarning() { base.DidReceiveMemoryWarning(); // Release any cached data, images, etc that aren't in use. } } public class NavigationDrawerDelegate : SFNavigationDrawerDelegate { public override void DidClose(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen; } public override void DidOpen(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen;base.DidOpen(navigationDrawer); } } }}
Step 2: Include the business logics inside the event handler which you have written in delegate class.
namespace DelegateToEvent{ public partial class ViewController : UIViewController { protected ViewController(IntPtr handle) : base(handle) { // Note: this .ctor should not contain any initialization logic. } public override void ViewDidLoad() { base.ViewDidLoad(); SFNavigationDrawer navigationDrawer = new SFNavigationDrawer(); navigationDrawer.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); UIView mainContentView = new UIView(); mainContentView.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); mainContentView.BackgroundColor = UIColor.FromRGBA(0, 0, 128, 0.2f); navigationDrawer.ContentView = mainContentView; UIView drawerContentView = new UIView(); drawerContentView.Frame = new CoreGraphics.CGRect(0, 0, 200, this.View.Frame.Height); drawerContentView.BackgroundColor = UIColor.FromRGBA(128, 0, 0, 0.8f); navigationDrawer.DrawerContentView = drawerContentView; navigationDrawer.DrawerHeaderHeight = 0; navigationDrawer.DrawerFooterHeight = 0; navigationDrawer.DrawerWidth = 200; navigationDrawer.Delegate = new NavigationDrawerDelegate(); //NavigationDrawer DidOpen EventHandler navigationDrawer.DidOpen += (sender, e) => { bool drawerState = navigationDrawer.IsOpen; }; //NavigationDrawer DidClose EventHandler navigationDrawer.DidClose += (sender, e) => { bool drawerState = navigationDrawer.IsOpen; }; this.View.AddSubview(navigationDrawer); } public override void DidReceiveMemoryWarning() { base.DidReceiveMemoryWarning(); // Release any cached data, images, etc that aren't in use. } } public class NavigationDrawerDelegate : SFNavigationDrawerDelegate { public override void DidClose(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen; } public override void DidOpen(SFNavigationDrawer navigationDrawer) { bool drawerState = navigationDrawer.IsOpen;base.DidOpen(navigationDrawer); } }}
Step 3: Remove the Delegate API and the Delegate class from the sample.
namespace DelegateToEvent{ public partial class ViewController : UIViewController { protected ViewController(IntPtr handle) : base(handle) { // Note: this .ctor should not contain any initialization logic. } public override void ViewDidLoad() { base.ViewDidLoad(); SFNavigationDrawer navigationDrawer = new SFNavigationDrawer(); navigationDrawer.Frame = new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); UIView mainContentView = new UIView(); mainContentView.Frame=new CoreGraphics.CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height); mainContentView.BackgroundColor = UIColor.FromRGBA(0, 0, 128, 0.2f); navigationDrawer.ContentView = mainContentView; UIView drawerContentView = new UIView(); drawerContentView.Frame = new CoreGraphics.CGRect(0, 0, 200, this.View.Frame.Height); drawerContentView.BackgroundColor = UIColor.FromRGBA(128, 0,0 , 0.8f); navigationDrawer.DrawerContentView = drawerContentView; navigationDrawer.DrawerHeaderHeight = 0; navigationDrawer.DrawerFooterHeight = 0; navigationDrawer.DrawerWidth = 200; //Comment the delegate assigning //navigationDrawer.Delegate = new NavigationDrawerDelegate(); //NavigationDrawer DidOpen EventHandler navigationDrawer.DidOpen += (sender, e) => { bool drawerState = navigationDrawer.IsOpen; }; //NavigationDrawer DidClose EventHandler navigationDrawer.DidClose += (sender, e) => { bool drawerState = navigationDrawer.IsOpen; }; this.View.AddSubview(navigationDrawer); } public override void DidReceiveMemoryWarning() { base.DidReceiveMemoryWarning(); // Release any cached data, images, etc that aren't in use. } }}