@@ -21,6 +21,9 @@ public class VideoView : NativeControlHost
21
21
private IPlatformHandle ? _platformHandle = null ;
22
22
private MediaPlayer ? _mediaPlayer = null ;
23
23
private Window ? _floatingContent = null ;
24
+ IDisposable ? contentChangedHandler = null ;
25
+ IDisposable ? isVisibleChangedHandler = null ;
26
+ IDisposable ? floatingContentChangedHandler = null ;
24
27
25
28
/// <summary>
26
29
/// MediaPlayer Data Bound property
@@ -74,15 +77,17 @@ public object? Content
74
77
public VideoView ( )
75
78
{
76
79
Initialized += ( _ , _ ) => { Attach ( ) ; } ;
77
- ContentProperty . Changed . AddClassHandler < VideoView > ( ( s , _ ) => s . InitializeNativeOverlay ( ) ) ;
78
- IsVisibleProperty . Changed . AddClassHandler < VideoView > ( ( s , _ ) => s . ShowNativeOverlay ( s . IsVisible ) ) ;
80
+
81
+ contentChangedHandler = ContentProperty . Changed . AddClassHandler < VideoView > ( ( s , _ ) => s . UpdateOverlayPosition ( ) ) ;
82
+ isVisibleChangedHandler = IsVisibleProperty . Changed . AddClassHandler < VideoView > ( ( s , _ ) => s . ShowNativeOverlay ( s . IsVisible ) ) ;
79
83
}
80
84
81
85
private void UpdateOverlayPosition ( )
82
86
{
83
87
if ( _floatingContent == null || ! IsVisible )
88
+ {
84
89
return ;
85
-
90
+ }
86
91
bool forceSetWidth = false , forceSetHeight = false ;
87
92
var topLeft = new Point ( ) ;
88
93
var child = _floatingContent . Presenter ? . Child ;
@@ -250,21 +255,21 @@ private void InitializeNativeOverlay()
250
255
Opacity = 1.0 ,
251
256
DataContext = DataContext
252
257
} ;
253
- _floatingContent . Bind ( ContentControl . ContentProperty , this . GetObservable ( ContentProperty ) ) ;
258
+ floatingContentChangedHandler = _floatingContent . Bind ( ContentControl . ContentProperty , this . GetObservable ( ContentProperty ) ) ;
254
259
_floatingContent . PointerEntered += FloatingContentOnPointerEvent ;
255
260
_floatingContent . PointerExited += FloatingContentOnPointerEvent ;
256
261
_floatingContent . PointerPressed += FloatingContentOnPointerEvent ;
257
262
_floatingContent . PointerReleased += FloatingContentOnPointerEvent ;
258
263
259
- ContentProperty . Changed . AddClassHandler < VideoView > ( ( o , _ ) => o . UpdateOverlayPosition ( ) ) ;
260
-
261
- visualRoot . LayoutUpdated += ( _ , _ ) => UpdateOverlayPosition ( ) ;
262
- visualRoot . PositionChanged += ( _ , _ ) => UpdateOverlayPosition ( ) ;
264
+ visualRoot . LayoutUpdated += VisualRoot_UpdateOverlayPosition ;
265
+ visualRoot . PositionChanged += VisualRoot_UpdateOverlayPosition ;
263
266
}
264
267
265
268
ShowNativeOverlay ( IsEffectivelyVisible ) ;
266
269
}
267
270
271
+ private void VisualRoot_UpdateOverlayPosition ( object sender , EventArgs e ) => UpdateOverlayPosition ( ) ;
272
+
268
273
private void FloatingContentOnPointerEvent ( object ? sender , PointerEventArgs e )
269
274
{
270
275
RaiseEvent ( e ) ;
@@ -284,13 +289,30 @@ private void ShowNativeOverlay(bool show)
284
289
/// <inheritdoc />
285
290
protected override void OnAttachedToVisualTree ( VisualTreeAttachmentEventArgs e )
286
291
{
292
+ var parent = this . GetVisualParent ( ) ;
293
+ if ( parent != null )
294
+ parent . DetachedFromVisualTree += Parent_DetachedFromVisualTree ;
287
295
base . OnAttachedToVisualTree ( e ) ;
288
296
InitializeNativeOverlay ( ) ;
289
297
}
290
298
299
+ private void Parent_DetachedFromVisualTree ( object sender , VisualTreeAttachmentEventArgs e )
300
+ {
301
+ if ( VisualRoot is not Window visualRoot )
302
+ {
303
+ return ;
304
+ }
305
+
306
+ visualRoot . LayoutUpdated -= VisualRoot_UpdateOverlayPosition ;
307
+ visualRoot . PositionChanged -= VisualRoot_UpdateOverlayPosition ;
308
+ }
309
+
291
310
/// <inheritdoc />
292
311
protected override void OnDetachedFromVisualTree ( VisualTreeAttachmentEventArgs e )
293
312
{
313
+ var parent = this . GetVisualParent ( ) ;
314
+ if ( parent != null )
315
+ parent . DetachedFromVisualTree -= Parent_DetachedFromVisualTree ;
294
316
base . OnDetachedFromVisualTree ( e ) ;
295
317
ShowNativeOverlay ( false ) ;
296
318
}
@@ -305,6 +327,10 @@ protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle paren
305
327
/// <inheritdoc />
306
328
protected override void DestroyNativeControlCore ( IPlatformHandle control )
307
329
{
330
+ contentChangedHandler ? . Dispose ( ) ;
331
+ isVisibleChangedHandler ? . Dispose ( ) ;
332
+ floatingContentChangedHandler ? . Dispose ( ) ;
333
+
308
334
Detach ( ) ;
309
335
if ( _floatingContent != null )
310
336
{
0 commit comments