@@ -11,6 +11,7 @@ namespace Xamarin.CommunityToolkit.Behaviors
11
11
/// </summary>
12
12
public class ImpliedOrderGridBehavior : BaseBehavior < Grid >
13
13
{
14
+ bool [ ] [ ] ? usedMatrix ;
14
15
int rowCount ;
15
16
int columnCount ;
16
17
@@ -51,21 +52,24 @@ bool[][] InitMatrix()
51
52
rowCount = View . RowDefinitions . Count ;
52
53
if ( rowCount == 0 )
53
54
rowCount = 1 ;
55
+
54
56
columnCount = View . ColumnDefinitions . Count ;
55
57
if ( columnCount == 0 )
56
58
columnCount = 1 ;
59
+
57
60
var newMatrix = new bool [ rowCount ] [ ] ;
58
61
for ( var r = 0 ; r < rowCount ; r ++ )
59
62
newMatrix [ r ] = new bool [ columnCount ] ;
63
+
60
64
return newMatrix ;
61
65
}
62
66
63
- void FindNextCell ( out int rowIndex , out int columnIndex , out bool [ ] [ ] matrix )
67
+ void FindNextCell ( out int rowIndex , out int columnIndex )
64
68
{
65
- matrix = InitMatrix ( ) ;
69
+ usedMatrix ?? = InitMatrix ( ) ;
66
70
67
71
// Find the first available row
68
- var row = matrix . FirstOrDefault ( r => r . Any ( c => ! c ) ) ;
72
+ var row = usedMatrix . FirstOrDefault ( r => r . Any ( c => ! c ) ) ;
69
73
70
74
// If no row is found, set cell to origin and log
71
75
if ( row == null )
@@ -75,13 +79,13 @@ void FindNextCell(out int rowIndex, out int columnIndex, out bool[][] matrix)
75
79
columnIndex = Math . Max ( columnCount - 1 , 0 ) ;
76
80
return ;
77
81
}
78
- rowIndex = matrix . IndexOf ( row ) ;
82
+ rowIndex = usedMatrix . IndexOf ( row ) ;
79
83
80
84
// Find the first available column
81
85
columnIndex = row . IndexOf ( row . FirstOrDefault ( c => ! c ) ) ;
82
86
}
83
87
84
- void UpdateUsedCells ( int row , int column , int rowSpan , int columnSpan , bool [ ] [ ] usedMatrix )
88
+ void UpdateUsedCells ( int row , int column , int rowSpan , int columnSpan )
85
89
{
86
90
var rowEnd = row + rowSpan ;
87
91
var columnEnd = column + columnSpan ;
@@ -102,8 +106,9 @@ void UpdateUsedCells(int row, int column, int rowSpan, int columnSpan, bool[][]
102
106
{
103
107
for ( var c = column ; c < columnEnd ; c ++ )
104
108
{
105
- if ( usedMatrix [ r ] [ c ] )
109
+ if ( usedMatrix ? [ r ] [ c ] ?? throw new NullReferenceException ( ) )
106
110
LogWarning ( $ "Cell at row { r } column { c } has already been used.") ;
111
+
107
112
usedMatrix [ r ] [ c ] = true ;
108
113
}
109
114
}
@@ -114,15 +119,15 @@ void ProcessElement(BindableObject view)
114
119
var columnSpan = Grid . GetColumnSpan ( view ) ;
115
120
var rowSpan = Grid . GetRowSpan ( view ) ;
116
121
117
- FindNextCell ( out var row , out var column , out var usedMatrix ) ;
122
+ FindNextCell ( out var row , out var column ) ;
118
123
119
124
// Check to see if the user manually assigned a row or column
120
125
if ( view . IsSet ( Grid . ColumnProperty ) )
121
126
column = Grid . GetColumn ( view ) ;
122
127
if ( view . IsSet ( Grid . RowProperty ) )
123
128
row = Grid . GetRow ( view ) ;
124
129
125
- UpdateUsedCells ( row , column , rowSpan , columnSpan , usedMatrix ) ;
130
+ UpdateUsedCells ( row , column , rowSpan , columnSpan ) ;
126
131
127
132
// Set attributes
128
133
view . SetValue ( Grid . ColumnProperty , column ) ;
0 commit comments