如何在Silverlight中使用MeasureOverride?

问题描述

| 我想我已经了解MeasureOverrride的工作原理,但是我试图在一个非常简单的情况下使用它,但它不起作用...所以现在我不太确定...使用Measureoverride之后,我必须也使用Arrageoverride,否则系统会为我做到吗?情况就是这样:我有一个从Panel继承的LinearLayout类,它有两个字段称为wrapwidht和wrapheigh,如果它们为true,则LinearLayout的宽度或高度必须满足其子级的要求。所以我的Measureoveride看起来像:
protected override Size MeasureOverride(Size availableSize) {

    Size panelDesiredSize = new Size();


    if ((this.widthWrap) || (this.heightWrap))
    {

        foreach (UIElement elemento in this.Children)
        {

            System.Diagnostics.Debug.WriteLine(((FrameworkElement)elemento).DesiredSize.ToString());

            if (this.Orientation.Equals(System.Windows.Controls.Orientation.Vertical))
            {
                if (this.widthWrap)
                {
                    //the widest element will determine containers width
                    if (panelDesiredSize.Width < ((FrameworkElement)elemento).Width)
                        panelDesiredSize.Width = ((FrameworkElement)elemento).Width;
                }
                //the height of the Layout is determine by the sum of all the elment that it cointains
                if (this.heightWrap)
                    panelDesiredSize.Height += ((FrameworkElement)elemento).Height;
            }
            else
            {
                if (this.heightWrap)
                {
                    //The highest will determine the height of the Layout
                    if (panelDesiredSize.Height < ((FrameworkElement)elemento).Height)
                        panelDesiredSize.Height = ((FrameworkElement)elemento).Height;
                }

                //The width of the container is the sum of all the elements widths
                if (this.widthWrap)
                    panelDesiredSize.Width += ((FrameworkElement)elemento).Width;
            }

        }
    }

    System.Diagnostics.Debug.WriteLine(\"desiredsizzeeeeeee\" + panelDesiredSize);

    return panelDesiredSize;

}
我要加入LinerLayout的孩子是3个按钮,但是什么也没画。.即使panelDesiredSize字段是正确的..所以也许我不明白它的工作原理。如果有人可以帮助我,我会非常好:-)     

解决方法

在与您相似的上一篇文章中查看我的答案:WPF和Silverlight中的两遍布局系统 答案是不,您不必重写ArrangeOverride,但是如果您不打算使用ArrangeOverride,那么使用MeasureOverride有什么意义呢?     ,您应该在子级上调用Measure方法。请参阅此处的示例:http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.measureoverride.aspx     ,您必须在\“ elemento \”上调用Measure。在Measure中,Silverlight会创建在elemento \模板中声明的UI元素,因为需要它们来实际测量并提供所需的大小。然后,您应该使用elemento.DesiredSize.Width和Height来为panelDesiredSize设置所需的大小。