2020-05-19 03:56:25 +03:00
|
|
|
using System;
|
|
|
|
using global::Windows.Foundation;
|
|
|
|
using global::Windows.UI.Xaml;
|
|
|
|
using global::Windows.UI.Xaml.Controls;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-05-19 03:56:25 +03:00
|
|
|
namespace System.Maui.Platform.UWP
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-05-19 03:56:25 +03:00
|
|
|
public class ViewToRendererConverter : global::Windows.UI.Xaml.Data.IValueConverter
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
public object Convert(object value, Type targetType, object parameter, string language)
|
|
|
|
{
|
|
|
|
var view = value as View;
|
|
|
|
if (view == null)
|
|
|
|
{
|
|
|
|
var page = value as Page;
|
|
|
|
if (page != null)
|
|
|
|
{
|
|
|
|
IVisualElementRenderer renderer = page.GetOrCreateRenderer();
|
|
|
|
return renderer.ContainerElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return new WrapperControl(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:46:03 +03:00
|
|
|
internal class WrapperControl : Panel
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
readonly View _view;
|
|
|
|
|
2017-02-03 21:13:53 +03:00
|
|
|
FrameworkElement FrameworkElement { get; }
|
2016-08-30 21:12:27 +03:00
|
|
|
|
2019-09-18 01:26:56 +03:00
|
|
|
internal void CleanUp()
|
|
|
|
{
|
|
|
|
_view?.Cleanup();
|
|
|
|
|
|
|
|
if(_view != null)
|
|
|
|
_view.MeasureInvalidated -= OnMeasureInvalidated;
|
|
|
|
}
|
2018-04-30 19:46:03 +03:00
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public WrapperControl(View view)
|
|
|
|
{
|
|
|
|
_view = view;
|
2019-09-18 01:26:56 +03:00
|
|
|
_view.MeasureInvalidated += OnMeasureInvalidated;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
IVisualElementRenderer renderer = Platform.CreateRenderer(view);
|
|
|
|
Platform.SetRenderer(view, renderer);
|
|
|
|
|
2017-02-03 21:13:53 +03:00
|
|
|
FrameworkElement = renderer.ContainerElement;
|
|
|
|
Children.Add(renderer.ContainerElement);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
// make sure we re-measure once the template is applied
|
2016-08-30 21:12:27 +03:00
|
|
|
if (FrameworkElement != null)
|
|
|
|
{
|
|
|
|
FrameworkElement.Loaded += (sender, args) =>
|
|
|
|
{
|
|
|
|
// If the view is a layout (stacklayout, grid, etc) we need to trigger a layout pass
|
|
|
|
// with all the controls in a consistent native state (i.e., loaded) so they'll actually
|
|
|
|
// have Bounds set
|
|
|
|
(_view as Layout)?.ForceLayout();
|
|
|
|
InvalidateMeasure();
|
|
|
|
};
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:26:56 +03:00
|
|
|
void OnMeasureInvalidated(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
InvalidateMeasure();
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:56:25 +03:00
|
|
|
protected override global::Windows.Foundation.Size ArrangeOverride(global::Windows.Foundation.Size finalSize)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
_view.IsInNativeLayout = true;
|
|
|
|
Layout.LayoutChildIntoBoundingRegion(_view, new Rectangle(0, 0, finalSize.Width, finalSize.Height));
|
2018-11-28 21:08:56 +03:00
|
|
|
|
|
|
|
if (_view.Width <= 0 || _view.Height <= 0)
|
|
|
|
{
|
|
|
|
// Hide Panel when size _view is empty.
|
|
|
|
// It is necessary that this element does not overlap other elements when it should be hidden.
|
|
|
|
Opacity = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Opacity = 1;
|
|
|
|
FrameworkElement?.Arrange(new Rect(_view.X, _view.Y, _view.Width, _view.Height));
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
_view.IsInNativeLayout = false;
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:56:25 +03:00
|
|
|
protected override global::Windows.Foundation.Size MeasureOverride(global::Windows.Foundation.Size availableSize)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
Size request = _view.Measure(availableSize.Width, availableSize.Height, MeasureFlags.IncludeMargins).Request;
|
2017-12-05 21:49:58 +03:00
|
|
|
|
|
|
|
if (request.Height < 0)
|
|
|
|
{
|
|
|
|
request.Height = availableSize.Height;
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:56:25 +03:00
|
|
|
global::Windows.Foundation.Size result;
|
2016-03-22 23:02:25 +03:00
|
|
|
if (_view.HorizontalOptions.Alignment == LayoutAlignment.Fill && !double.IsInfinity(availableSize.Width) && availableSize.Width != 0)
|
|
|
|
{
|
2020-05-19 03:56:25 +03:00
|
|
|
result = new global::Windows.Foundation.Size(availableSize.Width, request.Height);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 03:56:25 +03:00
|
|
|
result = new global::Windows.Foundation.Size(request.Width, request.Height);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:00:03 +03:00
|
|
|
Layout.LayoutChildIntoBoundingRegion(_view, new Rectangle(0, 0, result.Width, result.Height));
|
2016-08-30 21:12:27 +03:00
|
|
|
|
|
|
|
FrameworkElement?.Measure(availableSize);
|
2018-04-30 19:46:03 +03:00
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|