Move the new Layout classes to Forms; make the new layouts work in CG (#13792)

* Move the new Layout classes to Forms; make the new layouts work in CG

* - net6 fix

* - change to vertical stack layout

* - revert handler factory changes

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
This commit is contained in:
E.Z. Hart 2021-02-20 04:55:26 -07:00 коммит произвёл GitHub
Родитель 8e331e333b
Коммит 4ca457d330
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
19 изменённых файлов: 126 добавлений и 35 удалений

Просмотреть файл

@ -298,6 +298,7 @@ namespace Xamarin.Forms.Controls
}
List<GalleryPageFactory> _pages = new List<GalleryPageFactory> {
new GalleryPageFactory(() => new GalleryPages.LayoutGalleries.LayoutGallery(), ".NET MAUI Layouts"),
new GalleryPageFactory(() => new TabIndexTest.TabIndex(), "Accessibility TabIndex (2)"),
new GalleryPageFactory(() => new PlatformTestsConsole(), "Platform Automated Tests"),
new GalleryPageFactory(() => new EmbeddedFonts(), "Embedded Fonts"),

Просмотреть файл

@ -0,0 +1,17 @@
namespace Xamarin.Forms.Controls.GalleryPages.LayoutGalleries
{
public class HorizontalStackLayoutGallery : ContentPage
{
public HorizontalStackLayoutGallery()
{
var layout = new HorizontalStackLayout();
for (int n = 0; n < 3; n++)
{
layout.Add(new Label { Text = $"Label {n} in a horizontal stack" });
}
Content = layout;
}
}
}

Просмотреть файл

@ -0,0 +1,20 @@
namespace Xamarin.Forms.Controls.GalleryPages.LayoutGalleries
{
public class LayoutGallery : ContentPage
{
public LayoutGallery()
{
Content = new ScrollView
{
Content = new StackLayout
{
Children =
{
GalleryBuilder.NavButton("VerticalStackLayout Gallery", () => new VerticalStackLayoutGallery(), Navigation),
GalleryBuilder.NavButton("HorizontalStackLayout Gallery", () => new HorizontalStackLayoutGallery(), Navigation),
}
}
};
}
}
}

Просмотреть файл

@ -0,0 +1,17 @@
namespace Xamarin.Forms.Controls.GalleryPages.LayoutGalleries
{
public class VerticalStackLayoutGallery : ContentPage
{
public VerticalStackLayoutGallery()
{
var layout = new VerticalStackLayout();
for (int n = 0; n < 10; n++)
{
layout.Add(new Label { Text = $"Label {n} in a vertical stack" });
}
Content = layout;
}
}
}

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Platform.Handlers;
namespace Xamarin.Forms
{
@ -15,6 +16,9 @@ namespace Xamarin.Forms
//Xamarin.Platform.Registrar.Handlers.Register(typeof(Slider), typeof(Xamarin.Platform.Handlers.SliderHandler));
//Xamarin.Platform.Registrar.Handlers.Register(typeof(Button), typeof(Xamarin.Platform.Handlers.ButtonHandler));
//RegistrarHandlers.Handlers.Register<Xamarin.Forms.StackLayout, Xamarin.Platform.Handlers.LayoutHandler>();
Xamarin.Platform.Registrar.Handlers.Register<VerticalStackLayout, LayoutHandler>();
Xamarin.Platform.Registrar.Handlers.Register<HorizontalStackLayout, LayoutHandler>();
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
using Xamarin.Platform.Layouts;
namespace Xamarin.Forms
{
// Don't panic, Layout2.StackLayout is the temporary name for the abstract base class until
// we rename everything and move the legacy layouts
public class HorizontalStackLayout : Layout2.StackLayout
{
protected override ILayoutManager CreateLayoutManager() => new HorizontalStackLayoutManager(this);
}
}

Просмотреть файл

@ -1,11 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Platform;
using Xamarin.Platform.Handlers;
using Xamarin.Platform.Layouts;
namespace Sample
// This is a temporary namespace until we rename everything and move the legacy layouts
namespace Xamarin.Forms.Layout2
{
public abstract class Layout : View, Xamarin.Platform.ILayout, IEnumerable<IView>
{
@ -24,7 +23,7 @@ namespace Sample
IEnumerator IEnumerable.GetEnumerator() => _children.GetEnumerator();
public override Size Measure(double widthConstraint, double heightConstraint)
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
if (IsMeasureValid)
{
@ -39,7 +38,7 @@ namespace Sample
return DesiredSize;
}
public override void Arrange(Rectangle bounds)
protected override void ArrangeOverride(Rectangle bounds)
{
if (IsArrangeValid)
{
@ -53,7 +52,7 @@ namespace Sample
Handler?.SetFrame(Frame);
}
public override void InvalidateMeasure()
protected override void InvalidateMeasureOverride()
{
base.InvalidateMeasure();

Просмотреть файл

@ -1,8 +1,8 @@
using System.Linq;
using System.Runtime.InteropServices;
using Xamarin.Platform;
namespace Sample
// This is a temporary namespace until we rename everything and move the legacy layouts
namespace Xamarin.Forms.Layout2
{
public abstract class StackLayout : Layout, IStackLayout
{

Просмотреть файл

@ -0,0 +1,11 @@
using Xamarin.Platform.Layouts;
namespace Xamarin.Forms
{
// Don't panic, Layout2.StackLayout is the temporary name for the abstract base class until
// we rename everything and move the legacy layout
public class VerticalStackLayout : Layout2.StackLayout
{
protected override ILayoutManager CreateLayoutManager() => new VerticalStackLayoutManager(this);
}
}

Просмотреть файл

@ -434,7 +434,7 @@ namespace Xamarin.Forms
Padding = 6
};
BindToTemplatedParent(frame, BackgroundColorProperty, Frame.BorderColorProperty, HorizontalOptionsProperty,
BindToTemplatedParent(frame, BackgroundColorProperty, Xamarin.Forms.Frame.BorderColorProperty, HorizontalOptionsProperty,
MarginProperty, OpacityProperty, RotationProperty, ScaleProperty, ScaleXProperty, ScaleYProperty,
TranslationYProperty, TranslationXProperty, VerticalOptionsProperty);

Просмотреть файл

@ -176,7 +176,7 @@ namespace Xamarin.Forms
#region IView
Rectangle IFrameworkElement.Frame => Bounds;
public Rectangle Frame => Bounds;
public IViewHandler Handler
{
@ -194,7 +194,7 @@ namespace Xamarin.Forms
public Size DesiredSize { get; protected set; }
public bool IsMeasureValid { get; protected set; }
public virtual bool IsMeasureValid { get; protected set; }
public bool IsArrangeValid { get; protected set; }
@ -204,6 +204,13 @@ namespace Xamarin.Forms
}
void IFrameworkElement.Arrange(Rectangle bounds)
{
ArrangeOverride(bounds);
}
// ArrangeOverride provides a way to allow subclasses (e.g., Layout) to override Arrange even though
// the interface has to be explicitly implemented to avoid conflict with the old Arrange method
protected virtual void ArrangeOverride(Rectangle bounds)
{
if (IsArrangeValid)
return;
@ -218,10 +225,21 @@ namespace Xamarin.Forms
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (IsArrangeValid)
{
Handler?.SetFrame(Bounds);
}
}
Size IFrameworkElement.Measure(double widthConstraint, double heightConstraint)
{
return MeasureOverride(widthConstraint, heightConstraint);
}
// ArrangeOverride provides a way to allow subclasses (e.g., Layout) to override Measure even though
// the interface has to be explicitly implemented to avoid conflict with the old Measure method
protected virtual Size MeasureOverride(double widthConstraint, double heightConstraint)
{
if (!IsMeasureValid)
{
@ -233,6 +251,13 @@ namespace Xamarin.Forms
}
void IFrameworkElement.InvalidateMeasure()
{
InvalidateMeasureOverride();
}
// ArrangeOverride provides a way to allow subclasses (e.g., Layout) to override InvalidateMeasure even though
// the interface has to be explicitly implemented to avoid conflict with the VisualElement.InvalidateMeasure method
protected virtual void InvalidateMeasureOverride()
{
IsMeasureValid = false;
IsArrangeValid = false;

Просмотреть файл

@ -2,8 +2,8 @@
using UIKit;
using Xamarin.Platform;
#if !NET6_0
using Xamarin.Forms;
#if !NET6_0
using Xamarin.Forms.Platform.iOS;
#endif

Просмотреть файл

@ -1,9 +0,0 @@
using Xamarin.Platform.Layouts;
namespace Sample
{
public class HorizontalStackLayout : StackLayout
{
protected override ILayoutManager CreateLayoutManager() => new HorizontalStackLayoutManager(this);
}
}

Просмотреть файл

@ -1,9 +0,0 @@
using Xamarin.Platform.Layouts;
namespace Sample
{
public class VerticalStackLayout : StackLayout
{
protected override ILayoutManager CreateLayoutManager() => new VerticalStackLayoutManager(this);
}
}

Просмотреть файл

@ -22,8 +22,8 @@ namespace Sample
RegistrarHandlers.Handlers.Register<Button, ButtonHandler>();
RegistrarHandlers.Handlers.Register<Slider, SliderHandler>();
RegistrarHandlers.Handlers.Register<Sample.VerticalStackLayout, LayoutHandler>();
RegistrarHandlers.Handlers.Register<Sample.HorizontalStackLayout, LayoutHandler>();
RegistrarHandlers.Handlers.Register<VerticalStackLayout, LayoutHandler>();
RegistrarHandlers.Handlers.Register<HorizontalStackLayout, LayoutHandler>();
RegistrarHandlers.Handlers.Register<Xamarin.Forms.FlexLayout, LayoutHandler>();
RegistrarHandlers.Handlers.Register<Xamarin.Forms.StackLayout, LayoutHandler>();
//RegistrarHandlers.Handlers.Register<Entry, EntryHandler>();

Просмотреть файл

@ -25,6 +25,5 @@ namespace Xamarin.Platform
double Height { get; }
Thickness Margin { get; }
}
}

Просмотреть файл

@ -126,7 +126,7 @@ namespace Xamarin.Platform
{
var newObject = Activator.CreateInstance(handler);
if(newObject == null)
if (newObject == null)
throw new ArgumentException($"No Handler found for type: {type}", nameof(type));
return (TTypeRender)newObject;

Просмотреть файл

@ -51,7 +51,10 @@ namespace Xamarin.Forms
Element.PropertyChanged += OnElementPropertyChanged;
Element = element;
ViewHandler.SetVirtualView((IView)element);
((IView)element).Handler = ViewHandler;
if (Tracker == null)
{
Tracker = new VisualElementTracker(this);

Просмотреть файл

@ -39,6 +39,8 @@ namespace Xamarin.Forms.Platform.iOS
Element = element;
ViewHandler.SetVirtualView((IView)element);
((IView)element).Handler = ViewHandler;
ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(oldElement, Element));
}