@@ -24,7 +24,7 @@ public class PlatformTouchEff : PlatformEffect
2424 {
2525 public static void Preserve ( ) { }
2626
27-
27+
2828 private TouchEff _effect ;
2929 private bool _isHoverSupported ;
3030 private RippleDrawable _ripple ;
@@ -49,7 +49,7 @@ protected override void OnAttached()
4949 Control . Touch += OnTouch ;
5050 }
5151
52- if ( _effect . NativeAnimation )
52+ if ( _effect . NativeAnimation )
5353 {
5454 View . Clickable = true ;
5555 View . LongClickable = true ;
@@ -64,8 +64,10 @@ protected override void OnAttached()
6464 _ripple = CreateRipple ( ) ;
6565 _ripple . Radius = ( int ) ( View . Context . Resources . DisplayMetrics . Density * _effect . NativeAnimationRadius ) ;
6666 _viewOverlay . Background = _ripple ;
67-
68- Container . AddView ( _viewOverlay ) ;
67+ if ( Container != null )
68+ {
69+ Container . AddView ( _viewOverlay ) ;
70+ }
6971 _viewOverlay . BringToFront ( ) ;
7072 }
7173 }
@@ -76,18 +78,26 @@ protected override void OnDetached()
7678 {
7779 _effect . Control = null ;
7880 _effect = null ;
79- Container . LayoutChange -= LayoutChange ;
81+
8082 if ( Container != null )
8183 {
84+ Container . LayoutChange -= LayoutChange ;
8285 Container . Touch -= OnTouch ;
8386 }
87+
8488 if ( Control != null )
8589 {
90+ Control . LayoutChange -= LayoutChange ;
8691 Control . Touch -= OnTouch ;
8792 }
93+
8894 if ( _viewOverlay != null )
8995 {
90- Container . RemoveView ( _viewOverlay ) ;
96+ if ( Container != null )
97+ {
98+ Container . RemoveView ( _viewOverlay ) ;
99+ }
100+
91101 _viewOverlay . Pressed = false ;
92102 _viewOverlay . Foreground = null ;
93103 _viewOverlay . Dispose ( ) ;
@@ -104,6 +114,7 @@ protected override void OnDetached()
104114 private void OnTouch ( object sender , AView . TouchEventArgs e )
105115 {
106116 e . Handled = true ;
117+
107118 switch ( e . Event . ActionMasked )
108119 {
109120 case MotionEventActions . Down :
@@ -124,12 +135,12 @@ private void OnTouch(object sender, AView.TouchEventArgs e)
124135 HandleEnd ( TouchStatus . Canceled ) ;
125136 break ;
126137 case MotionEventActions . Move :
127- if ( _canceled )
138+ if ( _canceled )
128139 {
129140 return ;
130141 }
131- var diffX = Abs ( e . Event . GetX ( ) - _startX ) / Container . Context . Resources . DisplayMetrics . Density ;
132- var diffY = Abs ( e . Event . GetY ( ) - _startY ) / Container . Context . Resources . DisplayMetrics . Density ;
142+ var diffX = Abs ( e . Event . GetX ( ) - _startX ) / View . Context . Resources . DisplayMetrics . Density ;
143+ var diffY = Abs ( e . Event . GetY ( ) - _startY ) / View . Context . Resources . DisplayMetrics . Density ;
133144 var maxDiff = Max ( diffX , diffY ) ;
134145 var disallowTouchThreshold = _effect . DisallowTouchThreshold ;
135146 if ( disallowTouchThreshold > 0 && maxDiff > disallowTouchThreshold )
@@ -152,7 +163,7 @@ private void OnTouch(object sender, AView.TouchEventArgs e)
152163 if ( Element . GetTouchEff ( ) . Status != status )
153164 {
154165 Element . GetTouchEff ( ) . HandleTouch ( status ) ;
155- if ( status == TouchStatus . Started )
166+ if ( status == TouchStatus . Started )
156167 StartRipple ( e . Event . GetX ( ) , e . Event . GetY ( ) ) ;
157168 if ( status == TouchStatus . Canceled )
158169 EndRipple ( ) ;
@@ -174,7 +185,7 @@ private void OnTouch(object sender, AView.TouchEventArgs e)
174185
175186 private void HandleEnd ( TouchStatus status )
176187 {
177- if ( _canceled )
188+ if ( _canceled )
178189 {
179190 return ;
180191 }
0 commit comments