maui-linux/Xamarin.Forms.Platform.WinRT/VisualElementExtensions.cs

58 строки
1.2 KiB
C#

using System;
#if WINDOWS_UWP
namespace Xamarin.Forms.Platform.UWP
#else
namespace Xamarin.Forms.Platform.WinRT
#endif
{
public static class VisualElementExtensions
{
public static IVisualElementRenderer GetOrCreateRenderer(this VisualElement self)
{
if (self == null)
throw new ArgumentNullException("self");
IVisualElementRenderer renderer = Platform.GetRenderer(self);
if (renderer == null)
{
#pragma warning disable 618
renderer = RendererFactory.CreateRenderer(self);
#pragma warning restore 618
Platform.SetRenderer(self, renderer);
}
return renderer;
}
internal static void Cleanup(this VisualElement self)
{
if (self == null)
throw new ArgumentNullException("self");
IVisualElementRenderer renderer = Platform.GetRenderer(self);
foreach (Element element in self.Descendants())
{
var visual = element as VisualElement;
if (visual == null)
continue;
IVisualElementRenderer childRenderer = Platform.GetRenderer(visual);
if (childRenderer != null)
{
childRenderer.Dispose();
Platform.SetRenderer(visual, null);
}
}
if (renderer != null)
{
renderer.Dispose();
Platform.SetRenderer(self, null);
}
}
}
}