-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathMediaElementHandler.windows.cs
More file actions
77 lines (67 loc) · 2.51 KB
/
MediaElementHandler.windows.cs
File metadata and controls
77 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Handlers;
namespace CommunityToolkit.Maui.Core.Handlers;
public partial class MediaElementHandler : ViewHandler<MediaElement, MauiMediaElement>, IDisposable
{
/// <summary>
/// Maps the <see cref="Core.IMediaElement.ShouldLoopPlayback"/> property between the abstract
/// <see cref="MediaElement"/> and platform counterpart.
/// </summary>
/// <param name="handler">The associated handler.</param>
/// <param name="MediaElement">The associated <see cref="MediaElement"/> instance.</param>
public static void ShouldLoopPlayback(MediaElementHandler handler, MediaElement MediaElement)
{
handler?.MediaManager?.UpdateShouldLoopPlayback();
}
/// <inheritdoc/>
protected override MauiMediaElement CreatePlatformView()
{
MediaManager ??= new(MauiContext ?? throw new NullReferenceException(),
VirtualView,
Dispatcher.GetForCurrentThread() ?? throw new InvalidOperationException($"{nameof(IDispatcher)} cannot be null"));
var mediaPlatform = MediaManager.CreatePlatformView();
return new(mediaPlatform);
}
/// <summary>
/// Establishes a connection between the handler and the specified platform-specific media element view.
/// </summary>
/// <remarks>Overrides the base implementation to provide custom connection logic for the media element on the
/// Windows platform.</remarks>
/// <param name="platformView">The platform-specific media element view to connect to the handler. Cannot be null.</param>
protected override void ConnectHandler(MauiMediaElement platformView)
{
platformView.FullScreenStateChanged += OnScreenStateChanged;
base.ConnectHandler(platformView);
}
void OnScreenStateChanged(object? sender, ScreenStateChangedEventArgs e)
{
MediaManager?.UpdateFullScreenState(e.NewState);
}
/// <inheritdoc/>
protected override void DisconnectHandler(MauiMediaElement platformView)
{
platformView.FullScreenStateChanged -= OnScreenStateChanged;
Dispose();
UnloadPlatformView(platformView);
base.DisconnectHandler(platformView);
}
static void UnloadPlatformView(MauiMediaElement platformView)
{
if (platformView.IsLoaded)
{
platformView.Unloaded += OnPlatformViewUnloaded;
}
else
{
platformView.Dispose();
}
static void OnPlatformViewUnloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
var mediaElement = (MauiMediaElement)sender;
mediaElement.Unloaded -= OnPlatformViewUnloaded;
mediaElement.Dispose();
}
}
}