Skip to content

Commit b5c0ca7

Browse files
committed
Update 2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md
1 parent 6e5f25e commit b5c0ca7

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

_drafts/2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff 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
77

88
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.
99

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.
1111

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.
1313

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`:
1715

1816
```
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 =
32+
BindableProperty.Create(
33+
nameof(GridLinesColor),
34+
typeof(Color),
35+
typeof(PreviewGrid),
36+
Color.Default
37+
);
38+
39+
public bool ShowGridLines
40+
{
41+
get => (bool)GetValue(ShowGridLinesProperty);
42+
set => SetValue(ShowGridLinesProperty, value);
43+
}
44+
45+
public Color GridLinesColor
46+
{
47+
get => (Color)GetValue(GridLinesColorProperty);
48+
set => SetValue(GridLinesColorProperty, value);
49+
}
50+
51+
public PreviewGrid()
52+
{
53+
}
54+
}
55+
}
2056
```
2157

2258
```

0 commit comments

Comments
 (0)