You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _drafts/2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md
+42-6Lines changed: 42 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,16 +7,52 @@ I do a lot of work with Xamarin Forms. I'm in the XAML Previewer for much of my
7
7
8
8
One of the things about the Previewer that has always frustrated me is that it doesn't show the outlines of any of the objects that I place in a form.
9
9
10
-
The standard tactic to get around this limitation is to apply a `BackgroundColor` to each object being added. However, it's easy to see that this is ugly, prone to errors (I have accidentally shipped something with a `Fuchsia` (hot pink) background at least once) and extremely tedious.
10
+
One way to visualise the grid is to apply a `BackgroundColor` to each object being added. However, it's easy to see that this is ugly, prone to errors (I have accidentally shipped something with a `Fuchsia` (hot pink) background at least once) and extremely tedious.
11
11
12
-
After thinking about it for a while, I began to be convinced that "there must be a better way".
12
+
The search for a solution led initially to a custom renderer. Delving into the source of Xamarin Forms revealed that the `Grid` object is a subclass of `Layout<View>`, but it has no renderer of its own - all it does is manage a collection of child views and arrange them grid-fashion in its parent view.
13
13
14
-
Initially a custom renderer looked like a good idea. Delving into the source of Xamarin Forms revealed that the `Grid` object is a subclass of `Layout<View>`, but it has no renderer of its own - all it does is manage a collection of child views and arrange them grid-fashion in its parent view.
15
-
16
-
The next step was to subclass the `Grid` to `PreviewGrid` and create the custom `PreviewGridRenderer`:
14
+
To create the custom renderer, subclass the `Grid` to make a `PreviewGrid`:
17
15
18
16
```
19
-
PreviewGrid
17
+
using Xamarin.Forms;
18
+
19
+
namespace PreviewGrid.Views
20
+
{
21
+
public class PreviewGrid : Grid
22
+
{
23
+
public static readonly BindableProperty ShowGridLinesProperty =
24
+
BindableProperty.Create(
25
+
nameof(ShowGridLines),
26
+
typeof(bool),
27
+
typeof(PreviewGrid),
28
+
false
29
+
);
30
+
31
+
public static readonly BindableProperty GridLinesColorProperty =
0 commit comments