forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HandlerExtensions.cs
62 lines (46 loc) · 1.78 KB
/
HandlerExtensions.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
using System;
using Tizen.Applications;
using ElmSharp;
namespace Microsoft.Maui
{
public static class HandlerExtensions
{
public static EvasObject ToNative(this IElement view, IMauiContext context)
{
_ = view ?? throw new ArgumentNullException(nameof(view));
_ = context ?? throw new ArgumentNullException(nameof(context));
//This is how MVU works. It collapses views down
if (view is IReplaceableView ir)
view = ir.ReplacedView;
var handler = view.Handler;
if (handler?.MauiContext != null && handler.MauiContext != context)
handler = null;
if (handler == null)
handler = context.Handlers.GetHandler(view.GetType()) as IViewHandler;
if (handler == null)
throw new Exception($"Handler not found for view {view} or was not {nameof(IViewHandler)}.");
handler.SetMauiContext(context);
view.Handler = handler;
if (handler.VirtualView != view)
handler.SetVirtualView(view);
if (((INativeViewHandler)handler).NativeView is not EvasObject result)
throw new InvalidOperationException($"Unable to convert {view} to {typeof(EvasObject)}");
return result;
}
public static void SetWindow(this Window nativeWindow, IWindow window, IMauiContext mauiContext)
{
_ = nativeWindow ?? throw new ArgumentNullException(nameof(nativeWindow));
_ = window ?? throw new ArgumentNullException(nameof(window));
_ = mauiContext ?? throw new ArgumentNullException(nameof(mauiContext));
var handler = window.Handler;
if (handler == null)
handler = mauiContext.Handlers.GetHandler(window.GetType());
if (handler == null)
throw new Exception($"Handler not found for view {window}.");
handler.SetMauiContext(mauiContext);
window.Handler = handler;
if (handler.VirtualView != window)
handler.SetVirtualView(window);
}
}
}