2011-04-15 46 views
1

我已經創建了一個名爲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 ...

回答

1

測量階段將設置DesiredSize屬性,而不是寬度/高度屬性。寬度/高度屬性用於給元素一個明確的大小,而不是自動擬合它的內容(即通過MeasureOverride)。

您不應該根據度量或安排階段的結果設置寬度/高度,因爲度量將使用這些屬性,並安排階段以確定最佳大小。

UpdateLayout並不意味着強制元素重新測量/重新排列自己。您需要爲該任務使用InvalidateMeasureInvalidateArrange

編輯:

而且這行代碼是不是真的正確:

((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height)); 

你應該傳入availableSize,或者你的小組想給元素不論大小。寬度/高度可能不是有效的大小。

一般的經驗法則是您的面板不應該觸摸寬度/高度/ MinWidth/MinHeight/etc類型屬性。它應該只傳遞一個可用的大小,根據它提供的可用大小。或者當你安排給它一個足夠多的空間時,你可以通過new Size(double.PositiveInfinity, double.PositiveInfinity)

在您的安排階段,您可以使用element.DesiredSize來確定元素的排列矩形。

+0

我知道,但我的問題是DesiredSize沒有設置後,措施被稱爲..我不應該設置寬度和高度,但事情是,如果我不設置它們什麼都沒有發生,沒有任何東西被繪製... – Adriana 2011-04-15 14:28:01

+0

@Adriana - 我更新了我的答案,可能的問題。 – CodeNaked 2011-04-15 14:42:30

+0

非常感謝你mucchh !!!! :-) – Adriana 2011-04-20 10:19:52