以像素为单位获取可用UI空间的大小

时间:2010-12-30 15:38:48

标签: silverlight windows-phone-7

我的WP7应用程序中有以下Pivot控件。

<Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->
    <controls:Pivot Title="foo" >
        <controls:PivotItem Header="Item1">
            <Grid>
                <Image x:Name="imgItem1" Source="{Binding Path=Item1ImageUri}" Stretch="None" />
            </Grid>
        </controls:PivotItem>

        <!-- ... more PivotItems -->

    </controls:Pivot>
</Grid>

我希望imgItem1图片填充页面上的可用空间。 在我的代码中,我已经覆盖了OnNavigatedTo方法,我从Web服务请求图像。我可以在URI中为web服务提供大小参数。我想找到图像的可用大小(以像素为单位)并将这些值发送到Web服务。

我认为(int)imgItem1.ActualHeight(int)imgItem1.ActualWidth会很好看,但是在分配图像之前它们是0,然后它们就是图像的高度和宽度。

如何找到要加载的图像的最大/可用尺寸?

2 个答案:

答案 0 :(得分:1)

试试这个: -

<Grid>
    <Rectangle x:Name="rectangleMeasure" Fill="Transparent" />
    <Image x:Name="imgItem1" Source="{Binding Path=Item1ImageUri}" Stretch="None" />  
</Grid>

使用矩形的ActualHeight和ActualWidth来确定要包含在网址中的尺寸值。

答案 1 :(得分:0)

您还可以从Loaded事件中引用ActualHeight和ActualWidth页面。

e.g。

       private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
            System.Diagnostics.Debug.WriteLine(this.ActualHeight);
       }

请注意,如果启用了SystemTray,则任一种方法都将产生768而不是800。

       shell:SystemTray.IsVisible="True"