我已經創建了一個名爲LinearLayout的新面板,並且管理佈局我遇到問題,因爲在調用MeasureOverride方法之後字段desiredSize沒有被設置...這裏是我的代碼:MeasureOverride方法不會設置面板的desiredSize
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("Measure" + ((FrameworkElement)elemento).Name);
((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height));
System.Diagnostics.Debug.WriteLine(" child this desireddSIze" + ((FrameworkElement)elemento).DesiredSize);
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).DesiredSize.Width;
}
}
}
return panelDesiredSize;
}
我嵌套兩個線性佈局:L1是L2的父親,L2是3個按鈕的父親。
而有趣的是,如果我在類LinearLayout中的任何地方編寫代碼,它的工作原理,並再次調用佈局機制,它工作正常..但如果我調用UpdateLayout()什麼也沒有發生,機制是未激活(ANS的MeasureOverride被arrangeoverride不叫)
this.Width = finalSize.Width;
this.Height = finalSize.Height;
這是一個錯誤?或者只是我?我已經兩天了,它似乎爲其他人... 如果有人可以幫助我,我會很感激它! 順便說一下,我正在使用Windows Phone 7的Silverlight ...
我知道,但我的問題是DesiredSize沒有設置後,措施被稱爲..我不應該設置寬度和高度,但事情是,如果我不設置它們什麼都沒有發生,沒有任何東西被繪製... – Adriana 2011-04-15 14:28:01
@Adriana - 我更新了我的答案,可能的問題。 – CodeNaked 2011-04-15 14:42:30
非常感謝你mucchh !!!! :-) – Adriana 2011-04-20 10:19:52