Detect PointCollection changes in Polyline and Polygon (#11579) fixes #11848

This commit is contained in:
Javier Suárez 2020-11-27 16:46:14 +01:00 коммит произвёл GitHub
Родитель 84f7a54846
Коммит 56d47f4037
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 162 добавлений и 36 удалений

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

@ -36,12 +36,18 @@ namespace Xamarin.Forms.Controls.Issues
Text = "Tap the button, if the Polygon is updated, the test has passed."
};
var button = new Button
var updatePointsButton = new Button
{
Text = "Update Polygon points"
Text = "Update points"
};
var points = new PointCollection() { new Point(10, 10), new Point(100, 50), new Point(100, 95), new Point(10, 95) };
var updatePointsCollectionButton = new Button
{
Text = "Update points collection"
};
var points1 = new PointCollection() { new Point(10, 10), new Point(100, 50), new Point(100, 95), new Point(10, 95) };
var points2 = new PointCollection() { new Point(10, 5), new Point(100, 70), new Point(100, 95), new Point(10, 95) };
var polygon = new Polygon
{
@ -49,19 +55,28 @@ namespace Xamarin.Forms.Controls.Issues
WidthRequest = 100,
StrokeThickness = 2,
Stroke = Brush.Red,
Points = points
Points = points1
};
layout.Children.Add(instructions);
layout.Children.Add(button);
layout.Children.Add(updatePointsButton);
layout.Children.Add(updatePointsCollectionButton);
layout.Children.Add(polygon);
Content = layout;
button.Clicked += (sender, args) =>
updatePointsButton.Clicked += (sender, args) =>
{
if (points.Count > 1)
points.RemoveAt(1);
if (points1.Count > 1)
points1.RemoveAt(1);
if (points2.Count > 1)
points2.RemoveAt(1);
};
updatePointsCollectionButton.Clicked += (sender, args) =>
{
polygon.Points = points2;
};
}
}

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

@ -9,6 +9,8 @@ namespace Xamarin.Forms.Platform.Android
{
public class PolygonRenderer : ShapeRenderer<Polygon, PolygonView>
{
PointCollection _points;
public PolygonRenderer(Context context) : base(context)
{
@ -25,9 +27,6 @@ namespace Xamarin.Forms.Platform.Android
if (args.NewElement != null)
{
var points = args.NewElement.Points;
points.CollectionChanged += OnCollectionChanged;
UpdatePoints();
UpdateFillRule();
}
@ -49,17 +48,24 @@ namespace Xamarin.Forms.Platform.Android
if (disposing)
{
if (Element != null)
if (_points != null)
{
var points = Element.Points;
points.CollectionChanged -= OnCollectionChanged;
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
void UpdatePoints()
{
Control.UpdatePoints(Element.Points);
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.UpdatePoints(_points);
}
void UpdateFillRule()

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

@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Android.Content;
using Xamarin.Forms.Shapes;
using static Android.Graphics.Path;
@ -8,6 +9,8 @@ namespace Xamarin.Forms.Platform.Android
{
public class PolylineRenderer : ShapeRenderer<Polyline, PolylineView>
{
PointCollection _points;
public PolylineRenderer(Context context) : base(context)
{
@ -39,15 +42,41 @@ namespace Xamarin.Forms.Platform.Android
UpdateFillRule();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_points != null)
{
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
void UpdatePoints()
{
Control.UpdatePoints(Element.Points);
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.UpdatePoints(_points);
}
void UpdateFillRule()
{
Control.UpdateFillMode(Element.FillRule == FillRule.Nonzero);
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdatePoints();
}
}
public class PolylineView : ShapeView

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

@ -17,6 +17,8 @@ namespace Xamarin.Forms.Platform.WPF
{
public class PolygonRenderer : ShapeRenderer<Polygon, WPolygon>
{
PointCollection _points;
protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
{
if (Control == null && args.NewElement != null)
@ -52,17 +54,24 @@ namespace Xamarin.Forms.Platform.WPF
if (disposing)
{
if (Element != null)
if (_points != null)
{
var points = Element.Points;
points.CollectionChanged -= OnCollectionChanged;
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
void UpdatePoints()
{
Control.Points = Element.Points.ToWindows();
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.Points = _points.ToWindows();
}
void UpdateFillRule()

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

@ -1,5 +1,6 @@
using System.ComponentModel;
using Xamarin.Forms.Shapes;
using System.Collections.Specialized;
#if WINDOWS_UWP
using WFillRule = Windows.UI.Xaml.Media.FillRule;
@ -16,6 +17,8 @@ namespace Xamarin.Forms.Platform.WPF
{
public class PolylineRenderer : ShapeRenderer<Polyline, WPolyline>
{
PointCollection _points;
protected override void OnElementChanged(ElementChangedEventArgs<Polyline> args)
{
if (Control == null && args.NewElement != null)
@ -42,9 +45,30 @@ namespace Xamarin.Forms.Platform.WPF
UpdateFillRule();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_points != null)
{
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
void UpdatePoints()
{
Control.Points = Element.Points.ToWindows();
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.Points = _points.ToWindows();
}
void UpdateFillRule()
@ -53,5 +77,10 @@ namespace Xamarin.Forms.Platform.WPF
WFillRule.EvenOdd :
WFillRule.Nonzero;
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdatePoints();
}
}
}

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

@ -11,6 +11,8 @@ namespace Xamarin.Forms.Platform.MacOS
{
public class PolygonRenderer : ShapeRenderer<Polygon, PolygonView>
{
PointCollection _points;
[Internals.Preserve(Conditional = true)]
public PolygonRenderer()
{
@ -36,7 +38,7 @@ namespace Xamarin.Forms.Platform.MacOS
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
@ -46,23 +48,30 @@ namespace Xamarin.Forms.Platform.MacOS
UpdateFillRule();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (Element != null)
if (_points != null)
{
var points = Element.Points;
points.CollectionChanged -= OnCollectionChanged;
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
}
void UpdatePoints()
void UpdatePoints()
{
Control.UpdatePoints(Element.Points.ToCGPoints());
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.UpdatePoints(_points.ToCGPoints());
}
public void UpdateFillRule()

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

@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Specialized;
using System.ComponentModel;
using CoreGraphics;
using Xamarin.Forms.Shapes;
@ -10,6 +11,8 @@ namespace Xamarin.Forms.Platform.MacOS
{
public class PolylineRenderer : ShapeRenderer<Polyline, PolylineView>
{
PointCollection _points;
[Internals.Preserve(Conditional = true)]
public PolylineRenderer()
{
@ -42,22 +45,48 @@ namespace Xamarin.Forms.Platform.MacOS
UpdateFillRule();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_points != null)
{
_points.CollectionChanged -= OnCollectionChanged;
_points = null;
}
}
}
void UpdatePoints()
{
Control.UpdatePoints(Element.Points.ToCGPoints());
if (_points != null)
_points.CollectionChanged -= OnCollectionChanged;
_points = Element.Points;
_points.CollectionChanged += OnCollectionChanged;
Control.UpdatePoints(_points.ToCGPoints());
}
public void UpdateFillRule()
{
Control.UpdateFillMode(Element.FillRule == FillRule.Nonzero);
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdatePoints();
}
}
public class PolylineView : ShapeView
{
public void UpdatePoints(CGPoint[] points)
{
var path = new CGPath();
var path = new CGPath();
path.AddLines(points);
ShapeLayer.UpdateShape(path);
}
@ -67,4 +96,4 @@ namespace Xamarin.Forms.Platform.MacOS
ShapeLayer.UpdateFillMode(fillMode);
}
}
}
}