如果我理解正确,你有一个 Canvas 占据了整个 PivotItem?像这样的东西:
<phone:Pivot>
<phone:Pivot.Items>
<phone:PivotItem Header="menu">
<Canvas x:Name="Canvas" />
</phone:PivotItem>
<phone:PivotItem Header="game" />
</phone:Pivot.Items>
</phone:Pivot>
如果是这样,检索画布的高度非常简单:
Debug.WriteLine(this.Canvas.ActualHeight);
注意:该ActualHeight
属性不会在Loaded
事件之前填充。因此,如果您尝试从构造函数或OnNavigatedTo
方法中读取它,则该属性将等于 0。
完整的 XAML 代码:
<phone:PhoneApplicationPage
x:Class="WP7ForumTest.Page3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
Loaded="Page_Loaded">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:Pivot>
<phone:Pivot.Items>
<phone:PivotItem Header="menu">
<Canvas x:Name="Canvas" />
</phone:PivotItem>
<phone:PivotItem Header="game" />
</phone:Pivot.Items>
</phone:Pivot>
</Grid>
</phone:PhoneApplicationPage>
和代码隐藏:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show(this.Canvas.ActualHeight.ToString());
}