-
Notifications
You must be signed in to change notification settings - Fork 5
/
ElementHandlerExtensions.cs
92 lines (74 loc) · 2.95 KB
/
ElementHandlerExtensions.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#nullable enable
#if IOS || MACCATALYST
using PlatformView = UIKit.UIView;
#elif ANDROID
using PlatformView = Android.Views.View;
#elif WINDOWS
using PlatformView = Microsoft.UI.Xaml.FrameworkElement;
#elif TIZEN
using PlatformView = ElmSharp.EvasObject;
#elif (NETSTANDARD || !PLATFORM)
using PlatformView = System.Object;
#endif
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Handlers;
using System.Threading.Tasks;
// Copy of Maui repo maui\src\Core\src\Handlers\ElementHandlerExtensions.cs
namespace Microsoft.Maui
{
static class ElementHandlerExtensions
{
#if WINDOWS
//public static PlatformView ToPlatform(this IElementHandler elementHandler) =>
// (elementHandler.VirtualView?.ToPlatform() as PlatformView) ??
// throw new InvalidOperationException($"Unable to convert {elementHandler} to {typeof(PlatformView)}");
#endif
public static IServiceProvider GetServiceProvider(this IElementHandler handler)
{
var context = handler.MauiContext ??
throw new InvalidOperationException($"Unable to find the context. The {nameof(ElementHandler.MauiContext)} property should have been set by the host.");
var services = context?.Services ??
throw new InvalidOperationException($"Unable to find the service provider. The {nameof(ElementHandler.MauiContext)} property should have been set by the host.");
return services;
}
public static T? GetService<T>(this IElementHandler handler, Type type)
{
var services = handler.GetServiceProvider();
var service = services.GetService(type);
return (T?)service;
}
public static T? GetService<T>(this IElementHandler handler)
{
var services = handler.GetServiceProvider();
var service = services.GetService<T>();
return service;
}
public static T GetRequiredService<T>(this IElementHandler handler, Type type)
where T : notnull
{
var services = handler.GetServiceProvider();
var service = services.GetRequiredService(type);
return (T)service;
}
public static T GetRequiredService<T>(this IElementHandler handler)
where T : notnull
{
var services = handler.GetServiceProvider();
var service = services.GetRequiredService<T>();
return service;
}
public static Task<T> InvokeAsync<T>(this IElementHandler handler, string commandName,
TaskCompletionSource<T> args)
{
handler?.Invoke(commandName, args);
return args.Task;
}
public static T InvokeWithResult<T>(this IElementHandler handler, string commandName,
RetrievePlatformValueRequest<T> args)
{
handler?.Invoke(commandName, args);
return args.Result;
}
}
}