summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 20:02:25 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-03-22 23:13:41 (GMT)
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs')
-rw-r--r--Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs77
1 files changed, 77 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs b/Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs
new file mode 100644
index 0000000..49b232a
--- /dev/null
+++ b/Xamarin.Forms.Platform.WP8/ViewToRendererConverter.cs
@@ -0,0 +1,77 @@
+´╗┐using System;
+using System.Globalization;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace Xamarin.Forms.Platform.WinPhone
+{
+ public class ViewToRendererConverter : System.Windows.Data.IValueConverter
+ {
+ public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+ {
+ var view = value as View;
+ if (view == null)
+ return null;
+
+ return new WrapperControl(view);
+ }
+
+ public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+ {
+ throw new NotSupportedException();
+ }
+
+ class WrapperControl : ContentControl
+ {
+ readonly View _view;
+
+ public WrapperControl(View view)
+ {
+ _view = view;
+ _view.MeasureInvalidated += (sender, args) => InvalidateMeasure();
+
+ IVisualElementRenderer visualElementRenderer = Platform.CreateRenderer(view);
+ Platform.SetRenderer(view, visualElementRenderer);
+ Content = visualElementRenderer.ContainerElement;
+
+ // make sure we re-measure once the template is applied
+ var frameworkElement = visualElementRenderer.ContainerElement as FrameworkElement;
+ if (frameworkElement != null)
+ {
+ frameworkElement.Loaded += (sender, args) =>
+ {
+ _view.InvalidateMeasure(InvalidationTrigger.MeasureChanged);
+ InvalidateMeasure();
+ };
+ }
+ }
+
+ protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
+ {
+ _view.IsInNativeLayout = true;
+ Layout.LayoutChildIntoBoundingRegion(_view, new Rectangle(0, 0, finalSize.Width, finalSize.Height));
+ _view.IsInNativeLayout = false;
+
+ var content = Content as FrameworkElement;
+ content?.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
+ return finalSize;
+ }
+
+ protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
+ {
+ var content = Content as FrameworkElement;
+ content?.Measure(availableSize);
+ Size request = _view.Measure(availableSize.Width, availableSize.Height, MeasureFlags.IncludeMargins).Request;
+
+ System.Windows.Size result;
+ if (_view.HorizontalOptions.Alignment == LayoutAlignment.Fill && !double.IsInfinity(availableSize.Width) && availableSize.Width != 0)
+ result = new System.Windows.Size(availableSize.Width, request.Height);
+ else
+ result = new System.Windows.Size(request.Width, request.Height);
+
+ _view.Layout(new Rectangle(0, 0, result.Width, result.Width));
+ return result;
+ }
+ }
+ }
+} \ No newline at end of file