private static void BuildGrid()
{
_squares = new List<Rectangle>();
var frame = Application.Current.RootVisual as Frame;
if (frame == null || VisualTreeHelper.GetChildrenCount(frame) == 0)
{
Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);
return;
}
var child = VisualTreeHelper.GetChild(frame, 0);
var childAsBorder = child as Border;
var childAsGrid = child as Grid;
if (childAsBorder != null)
{
// Not a pretty way to control the root visual, but I did not
// want to implement using a popup.
var content = childAsBorder.Child;
if (content == null)
{
Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);
return;
}
childAsBorder.Child = null;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Grid newGrid = new Grid();
childAsBorder.Child = newGrid;
newGrid.Children.Add(content);
PrepareGrid(frame, newGrid);
});
}
else if (childAsGrid != null)
{
PrepareGrid(frame, childAsGrid);
}
else
{
Debug.WriteLine("Dear developer:");
Debug.WriteLine("Unfortunately the design overlay feature requires that the root frame visual");
Debug.WriteLine("be a Border or a Grid. So the overlay grid just isn't going to happen.");
}
}