/
FormsContentLoader.cs
76 lines (65 loc) · 2.45 KB
/
FormsContentLoader.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
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Xamarin.Forms.Platform.WPF.Interfaces;
namespace Xamarin.Forms.Platform.WPF
{
public class FormsContentLoader : IContentLoader
{
public Task<object> LoadContentAsync(FrameworkElement parent, object oldContent, object newContent, CancellationToken cancellationToken)
{
VisualElement element = oldContent as VisualElement;
if (element != null)
{
element.Cleanup(); // Cleanup old content
}
if (!System.Windows.Application.Current.Dispatcher.CheckAccess())
throw new InvalidOperationException("UIThreadRequired");
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
return Task.Factory.StartNew(() => LoadContent(parent, newContent), cancellationToken, TaskCreationOptions.None, scheduler);
}
protected virtual object LoadContent(FrameworkElement parent, object page)
{
VisualElement visualElement = page as VisualElement;
if (visualElement != null)
{
var renderer = CreateOrResizeContent(parent, visualElement);
return renderer;
}
return null;
}
public void OnSizeContentChanged(FrameworkElement parent, object page)
{
VisualElement visualElement = page as VisualElement;
if (visualElement != null)
{
CreateOrResizeContent(parent, visualElement);
}
}
private object CreateOrResizeContent(FrameworkElement parent, VisualElement visualElement)
{
var renderer = Platform.GetOrCreateRenderer(visualElement);
//if (Debugger.IsAttached)
// Console.WriteLine("Page type : " + visualElement.GetType() + " (" + (visualElement as Page).Title + ") -- Parent type : " + visualElement.Parent.GetType() + " -- " + parent.ActualHeight + "H*" + parent.ActualWidth + "W");
var actualRect = new Rectangle(0, 0, parent.ActualWidth, parent.ActualHeight);
visualElement.Layout(actualRect);
// ControlTemplate adds an additional layer through which to send sizing changes.
var contentPage = visualElement as ContentPage;
if (contentPage?.ControlTemplate != null)
{
contentPage.Content?.Layout(actualRect);
}
else
{
var contentView = visualElement as ContentView;
if (contentView?.ControlTemplate != null)
contentView.Content?.Layout(actualRect);
}
IPageController pageController = visualElement.RealParent as IPageController;
if (pageController != null)
pageController.ContainerArea = actualRect;
return renderer.GetNativeElement();
}
}
}