1
- using System . Collections . Specialized ;
1
+ using System . Collections . ObjectModel ;
2
+ using System . Collections . Specialized ;
2
3
using System . ComponentModel ;
3
4
using System . Linq ;
4
5
using System . Windows . Controls ;
10
11
using Xamarin . Forms . Platform . WPF ;
11
12
12
13
[ assembly: ExportRenderer ( typeof ( DrawingView ) , typeof ( DrawingViewRenderer ) ) ]
14
+
13
15
namespace Xamarin . CommunityToolkit . Sample . WPF
14
16
{
15
17
public class DrawingViewRenderer : ViewRenderer < DrawingView , InkCanvas >
@@ -19,11 +21,11 @@ public class DrawingViewRenderer : ViewRenderer<DrawingView, InkCanvas>
19
21
protected override void OnElementPropertyChanged ( object sender , PropertyChangedEventArgs e )
20
22
{
21
23
base . OnElementPropertyChanged ( sender , e ) ;
22
- if ( e . PropertyName == DrawingView . PointsProperty . PropertyName )
24
+ if ( e . PropertyName == DrawingView . LinesProperty . PropertyName )
23
25
{
24
26
canvas ! . Strokes . StrokesChanged -= OnStrokesChanged ;
25
27
canvas . Strokes . Clear ( ) ;
26
- LoadPoints ( ) ;
28
+ LoadLines ( ) ;
27
29
canvas . Strokes . StrokesChanged += OnStrokesChanged ;
28
30
}
29
31
}
@@ -35,15 +37,15 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
35
37
{
36
38
canvas = new InkCanvas
37
39
{
38
- DefaultDrawingAttributes =
40
+ Background = Element . BackgroundColor . ToBrush ( ) ,
41
+ DefaultDrawingAttributes = new ( )
39
42
{
40
- Color = Element . LineColor . ToMediaColor ( ) ,
41
- Width = Element . LineWidth ,
42
- Height = Element . LineWidth
43
- } ,
44
- Background = Element . BackgroundColor . ToBrush ( )
43
+ Color = Element . DefaultLineColor . ToMediaColor ( ) ,
44
+ Width = Element . DefaultLineWidth ,
45
+ Height = Element . DefaultLineWidth
46
+ }
45
47
} ;
46
- Element . Points . CollectionChanged += OnCollectionChanged ;
48
+ Element . Lines . CollectionChanged += OnCollectionChanged ;
47
49
SetNativeControl ( canvas ) ;
48
50
49
51
canvas . Strokes . StrokesChanged += OnStrokesChanged ;
@@ -53,55 +55,96 @@ protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
53
55
if ( e . OldElement != null )
54
56
{
55
57
canvas ! . Strokes . StrokesChanged -= OnStrokesChanged ;
56
- Element ! . Points . CollectionChanged -= OnCollectionChanged ;
58
+ Element ! . Lines . CollectionChanged -= OnCollectionChanged ;
57
59
if ( Control != null )
58
60
Control . PreviewMouseDown -= OnPreviewMouseDown ;
59
61
}
60
62
}
61
63
62
- void OnCollectionChanged ( object sender , NotifyCollectionChangedEventArgs args ) => LoadPoints ( ) ;
64
+ void OnCollectionChanged ( object sender , NotifyCollectionChangedEventArgs args )
65
+ {
66
+ canvas ! . Strokes . StrokesChanged -= OnStrokesChanged ;
67
+ canvas . Strokes . Clear ( ) ;
68
+ LoadLines ( ) ;
69
+ canvas . Strokes . StrokesChanged += OnStrokesChanged ;
70
+ }
71
+
72
+ void OnPreviewMouseDown ( object sender , MouseButtonEventArgs e ) => Clear ( ) ;
63
73
64
- void OnPreviewMouseDown ( object sender , MouseButtonEventArgs e )
74
+ void Clear ( bool force = false )
65
75
{
66
- canvas ! . Strokes . Clear ( ) ;
67
- Element . Points . Clear ( ) ;
76
+ if ( ! Element . MultiLineMode || force )
77
+ {
78
+ canvas ! . Strokes . Clear ( ) ;
79
+ Element . Lines . Clear ( ) ;
80
+ }
68
81
}
69
82
70
83
void OnStrokesChanged ( object sender , StrokeCollectionChangedEventArgs e )
71
84
{
72
- Element . Points . CollectionChanged -= OnCollectionChanged ;
85
+ Element . Lines . CollectionChanged -= OnCollectionChanged ;
73
86
if ( e . Added . Count > 0 )
74
87
{
75
- var points = e . Added . First ( ) . StylusPoints . Select ( point => new Point ( point . X , point . Y ) ) ;
76
- Element . Points . Clear ( ) ;
77
- foreach ( var point in points )
78
- Element . Points . Add ( point ) ;
88
+ if ( ! Element . MultiLineMode )
89
+ {
90
+ Element . Lines . Clear ( ) ;
91
+ }
79
92
80
- if ( Element . Points . Count > 0 )
93
+ var lines = Element . MultiLineMode ? e . Added : new StrokeCollection ( ) { e . Added . First ( ) } ;
94
+
95
+ foreach ( var line in lines )
96
+ {
97
+ var points = line . StylusPoints . Select ( point => new Point ( point . X , point . Y ) ) . ToList ( ) ;
98
+ Element . Lines . Add ( new Line ( )
99
+ {
100
+ Points = new ObservableCollection < Point > ( points ) ,
101
+ LineColor = Color . FromRgba ( line . DrawingAttributes . Color . R , line . DrawingAttributes . Color . G ,
102
+ line . DrawingAttributes . Color . B , line . DrawingAttributes . Color . A ) ,
103
+ LineWidth = ( float ) line . DrawingAttributes . Width
104
+ } ) ;
105
+ }
106
+
107
+ if ( Element . Lines . Count > 0 )
81
108
{
82
- if ( Element . DrawingCompletedCommand ? . CanExecute ( null ) ?? false )
83
- Element . DrawingCompletedCommand . Execute ( Element . Points ) ;
109
+ var lastLine = Element . Lines . Last ( ) ;
110
+ if ( Element . DrawingLineCompletedCommand ? . CanExecute ( lastLine ) ?? false )
111
+ Element . DrawingLineCompletedCommand . Execute ( lastLine ) ;
84
112
}
85
113
86
114
if ( Element . ClearOnFinish )
87
115
{
88
- canvas ! . Strokes . StrokesChanged -= OnStrokesChanged ;
89
- canvas . Strokes . Clear ( ) ;
90
- canvas . Strokes . StrokesChanged += OnStrokesChanged ;
91
- Element . Points . Clear ( ) ;
116
+ Element . Lines . CollectionChanged -= OnCollectionChanged ;
117
+ Clear ( true ) ;
118
+ canvas ! . Strokes . StrokesChanged += OnStrokesChanged ;
92
119
}
93
120
}
94
121
95
- Element . Points . CollectionChanged += OnCollectionChanged ;
122
+ Element . Lines . CollectionChanged += OnCollectionChanged ;
96
123
}
97
124
98
- void LoadPoints ( )
125
+ void LoadLines ( )
99
126
{
100
- var stylusPoints = Element ? . Points . Select ( point => new StylusPoint ( point . X , point . Y ) ) . ToList ( ) ;
101
- if ( stylusPoints is { Count : > 0 } )
127
+ var lines = Element . MultiLineMode
128
+ ? Element . Lines
129
+ : Element . Lines . Any ( )
130
+ ? new ObservableCollection < Line > { Element . Lines . LastOrDefault ( ) }
131
+ : new ObservableCollection < Line > ( ) ;
132
+ foreach ( var line in lines )
102
133
{
103
- var stroke = new Stroke ( new StylusPointCollection ( stylusPoints ) , canvas ! . DefaultDrawingAttributes ) ;
104
- canvas . Strokes . Add ( stroke ) ;
134
+ var stylusPoints = line . Points . Select ( point => new StylusPoint ( point . X , point . Y ) ) . ToList ( ) ;
135
+ if ( stylusPoints is { Count : > 0 } )
136
+ {
137
+ var stroke = new Stroke ( new StylusPointCollection ( stylusPoints ) )
138
+ {
139
+ DrawingAttributes = new ( )
140
+ {
141
+ Color = line . LineColor . ToMediaColor ( ) ,
142
+ Width = line . LineWidth ,
143
+ Height = line . LineWidth
144
+ }
145
+ } ;
146
+ canvas ! . Strokes . Add ( stroke ) ;
147
+ }
105
148
}
106
149
}
107
150
}
0 commit comments