Thông tin tài liệu:
Essential Silverlight 3- P5: Khái quát Silverlight 3 không chỉ là lấp đầy với các chi tiết kỹ thuật, ví dụ rõ ràng,
và thực hiện lời khuyên rằng sẽ làm cho bất kỳ ứng dụng Silverlight tốt hơn, nhưng
Ashraf cũng bao gồm những hiểu biết rằng chỉ có thể đến từ một trong những nhà phát triển dẫn
của thời gian chạy Silverlight. Từ đồ họa, văn bản, để phương tiện truyền thông cuốn sách này-
có tất cả các thông tin cần thiết về thời gian chạy lõi 3 Silverlight....
Nội dung trích xuất từ tài liệu:
Essential Silverlight 3- P5
168 Chapter 7: Layout
Width=100.5
Height=100.5
/>
Furthermore, you learned that snapping Rectangle elements to integer
positions removes these seams as shown in Figure 7.17.
Figure 7.17: Pixel snapped rasterization
The problem introduced with an automatic layout system is that you no
longer determine the sizes and positions of elements in your code. For
example, if a StackPanel element contains elements that have non-integer
widths and heights (which is common with text), some backgrounds,
shapes, and images may be positioned at non-integer positions and might
generate either seams or blurry images.
To solve the seaming problem and produce sharper images, the
Silverlight layout system automatically rounds sizes of elements up to
the nearest integer value so that widths and positions typically remain
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Layout Elements 169
integers. You can turn off automatic layout rounding by setting the
UseLayoutRounding property on a UIElement to False.
Building a Custom Layout
In some cases, you may want different layout behavior than the built-in
layout elements provided by Silverlight. For example, suppose you want
an element that can stack elements horizontally until they no longer fit,
and then wrap the next elements into further horizontal stacks in new
rows as shown in Figure 7.18.
Figure 7.18: WrapPanel example
This type of layout is a WrapPanel element and is not in the Silverlight
3 installation. For these custom layout behaviors, you can write a custom
layout element.
The layout algorithm is a two-step process involving a measure pass
and an arrange pass. The measure pass asks each element how large it
would like to be. The arrange pass tells each element where to position
itself and its final available size. If an element wants to be larger than the
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
170 Chapter 7: Layout
area available, it is up to each layout element to determine the new size and
position of its child elements.
To implement your own layout, you must
1. Create a class for your layout that inherits from a Panel derived class.
2. Override the MeasureOverride method with an implementation that
walks the child elements and determines the desired size of your
layout element container. For example, with the WrapPanel layout,
you want to return the width and height after wrapping.
3. Override the A rrangeOverride method with an implementation that
positions child elements based on the final size available.
In the MeasureOverride method, it is often useful to measure a child with
infinite available space to determine how big the child would like to be.
An example implementation of the WrapPanel class that produces the
result shown in Figure 7.18 is
namespace WrapPanelExample
{
public class WrapPanel : Panel
{
//
// MeasureOverride implementation
//
protected override Size MeasureOverride(Size availableSize)
{
Size panelSize = new Size();
Size childMeasure = new Size();
//
// Keep track of the height and width of the current row
//
double currentRowWidth = 0;
double currentRowHeight = 0;
//
// Measure children to determine their natural size
// by calling Measure with size PositiveInfinity
//
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Layout Elements 171
childMeasure.Width = Double.PositiveInfinity;
childMeasure.Height = Double.PositiveInfinity;
foreach (UIElement child in Children)
{
//
// Measure the child to determine its size
//
child.Measure(childMeasure);
//
// If the current child is too big to fit on the
// current row, start a new row
//
if (child.DesiredSize.Width
+ currentRowWidth > availableSize.Width)
{
panelSize.Width = Math.Max(
panelSize.Width,
currentRowWidth
);
...