【问题标题】:How do you use MeasureOverride in Silverlight?如何在 Silverlight 中使用 MeasureOverride?
【发布时间】:2011-08-05 12:19:34
【问题描述】:

我想我已经了解 MeasureOverrride 的工作原理,但我试图在一个非常简单的情况下使用它,但它不起作用......所以现在我不太确定......使用 MeasureOverrride 后我有也使用 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 文件是正确的..所以也许我不明白它是如何工作的。如果有人可以帮助我会非常好:-)

【问题讨论】:

    标签: silverlight measureoverride


    【解决方案1】:

    您必须在“elemento”上调用 Measure。在 Measure 期间,Silverlight 创建在 elemento 模板中声明的 UI 元素,因为需要它们来实际测量并得出所需的大小。然后,您应该使用 elemento.DesiredSize.Width 和 Height 为您的 panelDesiredSize 提供所需的大小。

    【讨论】:

      【解决方案2】:

      在与您类似的上一篇帖子中查看我的答案:Two Pass Layout system in WPF and Silverlight

      答案是不,您没有必须覆盖 ArrangeOverride,但是如果您不打算使用 ArrangeOverride,那么使用 MeasureOverride 的意义何在?

      【讨论】:

      • 你是对的,但是我在 ArrangeOverride 中编写了相同的代码,也没有任何反应。它假设设置当前元素的大小并调用子项排列并将当前元素添加到树?此方法由当前元素的父级调用,不是吗?什么时候布局过程?抱歉,我读了很多书,但没有人说是谁在调用这个方法……结果是什么……
      【解决方案3】:

      您应该在孩子上调用 Measure 方法。请参阅此处的示例:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx

      【讨论】:

        猜你喜欢
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多