次の方法で共有


Tiling with VisualBrush - WPF Beta 2

It has been a while since I last blog about WPF (Avalon) related work. Well, here’s some updates on something I tried recently. I have some requirement lately to implement some GridLines for Diagrams. I first try to use OnRender and doing DrawLine inside of OnRender. I found that this is somehow slow when I have many shapes and lines around it.

So I want something similar to DrawLine and try setting Background property to a VisualBrush. XAML as follow:

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" > <Grid>
<Grid.Background>
<VisualBrush TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute" Viewbox="0,0,20,20"
ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Rectangle Stroke="Blue" StrokeThickness="0.1" Width="20" Height="20" />
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>
</Grid>
</Page>

And this will look like:

I found that this perform better than OnRender, this is solely based on my moving of many lines and shapes around the diagram. Of course this is not cheap either, a Visual is basically equivalent to an UIElement. So you can essentially do this:

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" > <Grid>
<Grid.Background>
<VisualBrush TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute" Viewbox="0,0,20,20"
ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Button/>
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>
</Grid>
</Page>

The above XAML will tile what it seems a bunch of Button. But in reality only 1 Button object is created then casting to Visual and repeat the same bits across the Grid as background. Of course you can also compose deeper XAML tree for the VisualBrush but according the PERF guidance document at www.designerslove.net/docs/ Optimizing%20Performance%20in%20the%20Windows%20Presentation%20Foundation.doc

If you are in Software Rendering Mode, either 16 bits, Hardware Acceleration turned off or using older graphics card (support DX7 and lower) then tiling can be very expensive. If your card supports DX9, you should be fine and running Hardware Acceleration unless you are trying to tile 3d rotating objects as background then it really depends on how much VRAM you have J.

The other thing to note about Visuals in VisualBrush is that there don’t have hittesting, no events, no focus and purely serve as an image to the Background. Finally remember to set TileMode property to Tile to enable tiling.