2014-01-29 39 views
1

我有一個基本問題。我有我的MainPage.xaml中這樣的代碼,WP8在運行時得到網格(內容面板)高度

<!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel Grid.Row="0" Margin="12,17,0,28"> 

      <TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" 
         Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Foreground="Black" /> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" > 

     </Grid> 
    </Grid> 

在我MainPage.xaml.cs中的文件,

public MainPage() 
     { 
      InitializeComponent(); 

      MessageBox.Show(ContentPanel.Height.ToString()); 
     } 

我的消息框返回NaN的價值,但我想獲得一個特定的值,而不是無限期的價值。

謝謝。

+1

使用void MainWindow_Loaded(object sender,RoutedEventArgs e) MessageBox.Show(ContentPanel.ActualHeight.ToString()); } –

回答

0

基礎上提出的建議,

public MainPage() 
     { 
      InitializeComponent(); 

      this.Loaded += new RoutedEventHandler(DisplayMessage); 
     } 

void DisplayMessage(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show(ContentPanel.ActualHeight.ToString()); 
     } 

這將返回我,我的內容面板的高度。

謝謝。

2

您應該檢查Loaded事件中內容面板的實際高度。在構造函數將爲NaN,因爲實際值尚未確定......

+1

相當正確,但是在InitializeComponent()之後的Constructor中ActualHeight將爲0。高度是NaN(不是數字),因爲網格高度設置爲自動。但你是正確的,檢查加載事件ActualHeight是OP可能要查找的。 – Romasz

相關問題