@@ -19,6 +19,7 @@ namespace DIPS.Xamarin.UI.iOS.ContextMenu
1919 internal class ContextMenuButtonRenderer : ButtonRenderer
2020 {
2121 private ContextMenuButton m_contextMenuButton ;
22+ private NSObject m_didEnterBackgroundNotificationObserver ;
2223 internal static void Initialize ( ) { }
2324
2425 protected override void OnElementChanged ( ElementChangedEventArgs < Button > e )
@@ -36,22 +37,21 @@ protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
3637 CreateMenu ( ) ; //Create the menu the first time so it shows up the first time the user taps the button
3738 Control . TouchDown += OnTouchDown ;
3839 Control . ShowsMenuAsPrimaryAction = true ;
39- NSNotificationCenter . DefaultCenter . AddObserver ( UIApplication . DidEnterBackgroundNotification , notification =>
40+ m_didEnterBackgroundNotificationObserver = NSNotificationCenter . DefaultCenter . AddObserver ( UIApplication . DidEnterBackgroundNotification , notification =>
4041 {
4142 Control . Menu = null ;
4243 Control . Menu = CreateMenu ( ) ; //Recreate the menu to close it, and to make it possible to re-open it in one tap after it went to the background
4344 } ) ;
4445 }
4546 }
4647 }
47- else
48- {
49- if ( Control != null )
50- {
51- Control . TouchDown -= OnTouchDown ;
52- NSNotificationCenter . DefaultCenter . RemoveObserver ( UIApplication . DidEnterBackgroundNotification ) ;
53- }
54- }
48+ }
49+
50+ protected override void Dispose ( bool disposing )
51+ {
52+ Control . TouchDown -= OnTouchDown ;
53+ NSNotificationCenter . DefaultCenter . RemoveObserver ( m_didEnterBackgroundNotificationObserver ) ;
54+ base . Dispose ( disposing ) ;
5555 }
5656
5757 private void OnTouchDown ( object sender , EventArgs e )
0 commit comments