我想我已经了解 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 归档是正确的..所以也许我不明白它是如何工作的。如果有人可以帮助我会非常好:-)