diff --git a/samples/XCT.Sample.WPF/DrawingViewRenderer.wpf.cs b/samples/XCT.Sample.WPF/DrawingViewRenderer.wpf.cs index c0c249a16..e6f927c49 100644 --- a/samples/XCT.Sample.WPF/DrawingViewRenderer.wpf.cs +++ b/samples/XCT.Sample.WPF/DrawingViewRenderer.wpf.cs @@ -1,4 +1,5 @@ -using System.Collections.Specialized; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Windows.Controls; @@ -10,6 +11,7 @@ using Xamarin.Forms.Platform.WPF; [assembly: ExportRenderer(typeof(DrawingView), typeof(DrawingViewRenderer))] + namespace Xamarin.CommunityToolkit.Sample.WPF { public class DrawingViewRenderer : ViewRenderer @@ -19,11 +21,11 @@ public class DrawingViewRenderer : ViewRenderer protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); - if (e.PropertyName == DrawingView.PointsProperty.PropertyName) + if (e.PropertyName == DrawingView.LinesProperty.PropertyName) { canvas!.Strokes.StrokesChanged -= OnStrokesChanged; canvas.Strokes.Clear(); - LoadPoints(); + LoadLines(); canvas.Strokes.StrokesChanged += OnStrokesChanged; } } @@ -35,15 +37,15 @@ protected override void OnElementChanged(ElementChangedEventArgs e) { canvas = new InkCanvas { - DefaultDrawingAttributes = + Background = Element.BackgroundColor.ToBrush(), + DefaultDrawingAttributes = new() { - Color = Element.LineColor.ToMediaColor(), - Width = Element.LineWidth, - Height = Element.LineWidth - }, - Background = Element.BackgroundColor.ToBrush() + Color = Element.DefaultLineColor.ToMediaColor(), + Width = Element.DefaultLineWidth, + Height = Element.DefaultLineWidth + } }; - Element.Points.CollectionChanged += OnCollectionChanged; + Element.Lines.CollectionChanged += OnCollectionChanged; SetNativeControl(canvas); canvas.Strokes.StrokesChanged += OnStrokesChanged; @@ -53,55 +55,96 @@ protected override void OnElementChanged(ElementChangedEventArgs e) if (e.OldElement != null) { canvas!.Strokes.StrokesChanged -= OnStrokesChanged; - Element!.Points.CollectionChanged -= OnCollectionChanged; + Element!.Lines.CollectionChanged -= OnCollectionChanged; if (Control != null) Control.PreviewMouseDown -= OnPreviewMouseDown; } } - void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) => LoadPoints(); + void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) + { + canvas!.Strokes.StrokesChanged -= OnStrokesChanged; + canvas.Strokes.Clear(); + LoadLines(); + canvas.Strokes.StrokesChanged += OnStrokesChanged; + } + + void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) => Clear(); - void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) + void Clear(bool force = false) { - canvas!.Strokes.Clear(); - Element.Points.Clear(); + if (!Element.MultiLineMode || force) + { + canvas!.Strokes.Clear(); + Element.Lines.Clear(); + } } void OnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e) { - Element.Points.CollectionChanged -= OnCollectionChanged; + Element.Lines.CollectionChanged -= OnCollectionChanged; if (e.Added.Count > 0) { - var points = e.Added.First().StylusPoints.Select(point => new Point(point.X, point.Y)); - Element.Points.Clear(); - foreach (var point in points) - Element.Points.Add(point); + if (!Element.MultiLineMode) + { + Element.Lines.Clear(); + } - if (Element.Points.Count > 0) + var lines = Element.MultiLineMode ? e.Added : new StrokeCollection() {e.Added.First()}; + + foreach (var line in lines) + { + var points = line.StylusPoints.Select(point => new Point(point.X, point.Y)).ToList(); + Element.Lines.Add(new Line() + { + Points = new ObservableCollection(points), + LineColor = Color.FromRgba(line.DrawingAttributes.Color.R, line.DrawingAttributes.Color.G, + line.DrawingAttributes.Color.B, line.DrawingAttributes.Color.A), + LineWidth = (float) line.DrawingAttributes.Width + }); + } + + if (Element.Lines.Count > 0) { - if (Element.DrawingCompletedCommand?.CanExecute(null) ?? false) - Element.DrawingCompletedCommand.Execute(Element.Points); + var lastLine = Element.Lines.Last(); + if (Element.DrawingLineCompletedCommand?.CanExecute(lastLine) ?? false) + Element.DrawingLineCompletedCommand.Execute(lastLine); } if (Element.ClearOnFinish) { - canvas!.Strokes.StrokesChanged -= OnStrokesChanged; - canvas.Strokes.Clear(); - canvas.Strokes.StrokesChanged += OnStrokesChanged; - Element.Points.Clear(); + Element.Lines.CollectionChanged -= OnCollectionChanged; + Clear(true); + canvas!.Strokes.StrokesChanged += OnStrokesChanged; } } - Element.Points.CollectionChanged += OnCollectionChanged; + Element.Lines.CollectionChanged += OnCollectionChanged; } - void LoadPoints() + void LoadLines() { - var stylusPoints = Element?.Points.Select(point => new StylusPoint(point.X, point.Y)).ToList(); - if (stylusPoints is { Count: > 0 }) + var lines = Element.MultiLineMode + ? Element.Lines + : Element.Lines.Any() + ? new ObservableCollection {Element.Lines.LastOrDefault()} + : new ObservableCollection(); + foreach (var line in lines) { - var stroke = new Stroke(new StylusPointCollection(stylusPoints), canvas!.DefaultDrawingAttributes); - canvas.Strokes.Add(stroke); + var stylusPoints = line.Points.Select(point => new StylusPoint(point.X, point.Y)).ToList(); + if (stylusPoints is {Count: > 0}) + { + var stroke = new Stroke(new StylusPointCollection(stylusPoints)) + { + DrawingAttributes = new() + { + Color = line.LineColor.ToMediaColor(), + Width = line.LineWidth, + Height = line.LineWidth + } + }; + canvas!.Strokes.Add(stroke); + } } } } diff --git a/samples/XCT.Sample/Pages/Views/DrawingViewPage.xaml b/samples/XCT.Sample/Pages/Views/DrawingViewPage.xaml index 36345608a..9ff847aea 100644 --- a/samples/XCT.Sample/Pages/Views/DrawingViewPage.xaml +++ b/samples/XCT.Sample/Pages/Views/DrawingViewPage.xaml @@ -6,22 +6,25 @@ xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages" xmlns:viewsVodel="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Views;assembly=Xamarin.CommunityToolkit.Sample"> - - - - - + + + +