Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit fa74c73

Browse files
authored
Detect Points changes in Polygon (#11576)
Co-authored-by: Rui Marinho <[email protected]> fixes #11563
1 parent 9d7e9d9 commit fa74c73

File tree

5 files changed

+146
-5
lines changed

5 files changed

+146
-5
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Xamarin.Forms.Internals;
2+
using Xamarin.Forms.CustomAttributes;
3+
using Xamarin.Forms.Shapes;
4+
using System.Collections.Generic;
5+
6+
#if UITEST
7+
using Xamarin.UITest;
8+
using NUnit.Framework;
9+
using Xamarin.Forms.Core.UITests;
10+
#endif
11+
12+
namespace Xamarin.Forms.Controls.Issues
13+
{
14+
#if UITEST
15+
[Category(UITestCategories.Shape)]
16+
#endif
17+
[Preserve(AllMembers = true)]
18+
[Issue(IssueTracker.Github, 11563,
19+
"[Bug] Polygon.Points doesn't respond to CollectionChanged events",
20+
PlatformAffected.All)]
21+
public class Issue11563 : TestContentPage
22+
{
23+
public Issue11563()
24+
{
25+
#if APP
26+
Device.SetFlags(new List<string> { ExperimentalFlags.BrushExperimental, ExperimentalFlags.ShapesExperimental });
27+
#endif
28+
}
29+
30+
protected override void Init()
31+
{
32+
var layout = new StackLayout();
33+
34+
var instructions = new Label
35+
{
36+
Padding = 12,
37+
BackgroundColor = Color.Black,
38+
TextColor = Color.White,
39+
Text = "Tap the button, if the Polygon is updated, the test has passed."
40+
};
41+
42+
var button = new Button
43+
{
44+
Text = "Update Polygon points"
45+
};
46+
47+
var points = new PointCollection() { new Point(10, 10), new Point(100, 50), new Point(100, 95), new Point(10, 95) };
48+
49+
var polygon = new Polygon
50+
{
51+
HeightRequest = 100,
52+
WidthRequest = 100,
53+
StrokeThickness = 2,
54+
Stroke = Brush.Red,
55+
Points = points
56+
};
57+
58+
layout.Children.Add(instructions);
59+
layout.Children.Add(button);
60+
layout.Children.Add(polygon);
61+
62+
Content = layout;
63+
64+
button.Clicked += (sender, args) =>
65+
{
66+
if (points.Count > 1)
67+
points.RemoveAt(1);
68+
};
69+
}
70+
}
71+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@
14621462
<Compile Include="$(MSBuildThisFileDirectory)Issue11291.cs" />
14631463
<Compile Include="$(MSBuildThisFileDirectory)Issue11244.cs" />
14641464
<Compile Include="$(MSBuildThisFileDirectory)Issue11272.cs" />
1465+
<Compile Include="$(MSBuildThisFileDirectory)Issue11563.cs" />
14651466
<Compile Include="$(MSBuildThisFileDirectory)Issue11547.xaml.cs" />
14661467
<Compile Include="$(MSBuildThisFileDirectory)Issue11247.cs" />
14671468
</ItemGroup>

Xamarin.Forms.Platform.Android/Shapes/PolygonRenderer.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System.Collections.Specialized;
2+
using System.ComponentModel;
23
using Android.Content;
34
using Xamarin.Forms.Shapes;
45
using static Android.Graphics.Path;
@@ -24,6 +25,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
2425

2526
if (args.NewElement != null)
2627
{
28+
var points = args.NewElement.Points;
29+
points.CollectionChanged += OnCollectionChanged;
30+
2731
UpdatePoints();
2832
UpdateFillRule();
2933
}
@@ -39,6 +43,20 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
3943
UpdateFillRule();
4044
}
4145

46+
protected override void Dispose(bool disposing)
47+
{
48+
base.Dispose(disposing);
49+
50+
if (disposing)
51+
{
52+
if (Element != null)
53+
{
54+
var points = Element.Points;
55+
points.CollectionChanged -= OnCollectionChanged;
56+
}
57+
}
58+
}
59+
4260
void UpdatePoints()
4361
{
4462
Control.UpdatePoints(Element.Points);
@@ -48,6 +66,11 @@ void UpdateFillRule()
4866
{
4967
Control.UpdateFillMode(Element.FillRule == FillRule.Nonzero);
5068
}
69+
70+
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
71+
{
72+
UpdatePoints();
73+
}
5174
}
5275

5376
public class PolygonView : ShapeView

Xamarin.Forms.Platform.UAP/Shapes/PolygonRenderer.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.ComponentModel;
22
using Xamarin.Forms.Shapes;
3+
using System.Collections.Specialized;
34

45
#if WINDOWS_UWP
56
using WFillRule = Windows.UI.Xaml.Media.FillRule;
@@ -27,6 +28,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
2728

2829
if (args.NewElement != null)
2930
{
31+
var points = args.NewElement.Points;
32+
points.CollectionChanged += OnCollectionChanged;
33+
3034
UpdatePoints();
3135
UpdateFillRule();
3236
}
@@ -42,6 +46,20 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
4246
UpdateFillRule();
4347
}
4448

49+
protected override void Dispose(bool disposing)
50+
{
51+
base.Dispose(disposing);
52+
53+
if (disposing)
54+
{
55+
if (Element != null)
56+
{
57+
var points = Element.Points;
58+
points.CollectionChanged -= OnCollectionChanged;
59+
}
60+
}
61+
}
62+
4563
void UpdatePoints()
4664
{
4765
Control.Points = Element.Points.ToWindows();
@@ -53,5 +71,10 @@ void UpdateFillRule()
5371
WFillRule.EvenOdd :
5472
WFillRule.Nonzero;
5573
}
74+
75+
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
76+
{
77+
UpdatePoints();
78+
}
5679
}
5780
}

Xamarin.Forms.Platform.iOS/Shapes/PolygonRenderer.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System.Collections.Specialized;
2+
using System.ComponentModel;
23
using CoreGraphics;
34
using Xamarin.Forms.Shapes;
45

@@ -27,12 +28,15 @@ protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
2728

2829
if (args.NewElement != null)
2930
{
31+
var points = args.NewElement.Points;
32+
points.CollectionChanged += OnCollectionChanged;
33+
3034
UpdatePoints();
3135
UpdateFillRule();
3236
}
3337
}
3438

35-
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
39+
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
3640
{
3741
base.OnElementPropertyChanged(sender, args);
3842

@@ -42,7 +46,21 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
4246
UpdateFillRule();
4347
}
4448

45-
void UpdatePoints()
49+
protected override void Dispose(bool disposing)
50+
{
51+
base.Dispose(disposing);
52+
53+
if (disposing)
54+
{
55+
if (Element != null)
56+
{
57+
var points = Element.Points;
58+
points.CollectionChanged -= OnCollectionChanged;
59+
}
60+
}
61+
}
62+
63+
void UpdatePoints()
4664
{
4765
Control.UpdatePoints(Element.Points.ToCGPoints());
4866
}
@@ -51,6 +69,11 @@ public void UpdateFillRule()
5169
{
5270
Control.UpdateFillMode(Element.FillRule == FillRule.Nonzero);
5371
}
72+
73+
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
74+
{
75+
UpdatePoints();
76+
}
5477
}
5578

5679
public class PolygonView : ShapeView
@@ -69,4 +92,4 @@ public void UpdateFillMode(bool fillMode)
6992
ShapeLayer.UpdateFillMode(fillMode);
7093
}
7194
}
72-
}
95+
}

0 commit comments

Comments
 (0)