Skip to content

Commit a69ec62

Browse files
committed
Update 2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md
1 parent 217d14b commit a69ec62

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ published: false
55

66
This blog post describes how to show make a Xamarin.Forms `Grid` show its columns and rows graphically in the Visual Studio XAML Preview.
77

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.
8+
One of the things about the XAML 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. It's much easier to visualise what's going on if we can see the objects in a form even when they have no content.
99

10-
A simple way to visualise the grid is to create some `BoxView` objects, give them `BackgroundColor`, and add them to the `Grid`. However, it's easy to see that this is ugly, prone to errors (I have accidentally shipped something with a hot pink background at least once) and extremely tedious.
10+
A simple way to visualise the grid is to create some `BoxView` objects, give them `BackgroundColor`, and add them to the `Grid` in any spots where the grid is not obvious.
1111

12-
Xamarin.Forms objects are cross-platform virtualisations that draw on each platform by means of a Renderer. Delving into the source of Xamarin Forms revealed that the `Grid` object is a subclass of `Layout<View>`, but unusually it has no renderer of its own. This is because all it does is manage a collection of child views and arrange them grid-fashion in its parent view.
12+
However, it's easy to see that this is ugly and prone to errors.
13+
14+
- It adds objects to the visual tree that are only needed at design time.
15+
- There is a significant risk that these objects will appear in the live app when we don't want them to.
16+
17+
To provide a good solution to these issues we need something that can display the details of a `Grid` at design-time, but is invisible in a live app.
18+
19+
Xamarin.Forms objects are cross-platform virtualisations that draw on each platform by means of a Renderer. Delving into the source of Xamarin.Forms shows that the `Grid` object is a subclass of `Layout<View>`, but unusually it has no renderer of its own. This is because all it does is manage a collection of child views and arrange them grid-fashion in its parent view.
1320

1421
Most Xamarin.Forms objects are open for subclassing. The `Grid` object definitely is, so we can subclass it in our platform-independent code to make a `PreviewGrid`:
1522

16-
```
23+
```csharp
1724
using Xamarin.Forms;
1825

1926
namespace PreviewGridLines.Views
@@ -53,7 +60,7 @@ namespace PreviewGridLines.Views
5360

5461
In your platform-specific projects you'll need a subclass of `ViewRenderer`. The one for iOS looks like this:
5562

56-
```
63+
```csharp
5764
using System.ComponentModel;
5865
using System.Linq;
5966
using CoreGraphics;
@@ -261,7 +268,7 @@ namespace PreviewGridLines.iOS.Renderers
261268

262269
Here is an example of what this looks like in practice:
263270

264-
```
271+
```xml
265272
<?xml version="1.0" encoding="utf-8"?>
266273
<ContentPage
267274
xmlns="http://xamarin.com/schemas/2014/forms"
@@ -275,7 +282,7 @@ Here is an example of what this looks like in practice:
275282
Padding="24"
276283
ColumnSpacing="12"
277284
RowSpacing="24"
278-
VerticalOptions="StartAndExpand"
285+
VerticalOptions="Start"
279286
IsShowingGridLines="true"
280287
GridLinesColor="Teal"
281288
>

0 commit comments

Comments
 (0)