locked
[W8.1][VB]DesiredSize not calculated when calling Measure method

    Question

  • I am trying to determine the DesiredSize of an element, but it is not being calculated even when I call the Measure method. I currently have the following code:
    pop.Child.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
    Dim childwidth As Double = pop.Child.DesiredSize.Width
    pop is a Popup, and pop.Child is a Grid. pop.Child (a Grid) has Width="400" at the beginning. When I run this code, I receive an ArgumentException with the message "Value does not fall within the expected range.". The value of DesiredSize at this point is {0,0}. Shouldn't pop.Child.DesiredSize.Width have a value of 400? Why is Measure not updating the values for DesiredSize? Any help would be appreciated. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/


    Thursday, April 30, 2015 10:09 PM

All replies

  • Hi Nathan,

    I tired to repro your issue in C#, but I can not reproduce the issue.

    My xaml code is quite simple:

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Popup VerticalOffset="10" HorizontalOffset="200" x:Name="StandardPopup">
                <Grid Width="400">
                    <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" 
                        Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
                        BorderThickness="2" Width="200" Height="200">
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock Text="Simple Popup" FontSize="24.667" HorizontalAlignment="Center" />
                            <Button Content="Close"  HorizontalAlignment="Center" />
                        </StackPanel>
                    </Border>
                </Grid>
            </Popup>
            <Button Content="Button" HorizontalAlignment="Left" Height="88" Margin="625,154,0,0" VerticalAlignment="Top" Width="96" Click="Button_Click"/>
        </Grid>

    I will further test the same thing by using VB.net

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 01, 2015 3:15 AM
    Moderator
  • Again, fine with VB

    I would like to know where you execute the code? I run it by a button click event, or when you execute the code, the pop is not ready yet, perhaps.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 01, 2015 3:22 AM
    Moderator
  • I run the code from in an Extension method, which is the following:
    <Extension()> Public Sub HorizontalOffsetRight(pop As Popup)
    	If pop IsNot Nothing AndAlso pop.Child IsNot Nothing Then
    		'Determine the width of pop.Child
    		pop.Child.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
    		Dim childwidth As Double = pop.Child.DesiredSize.Width
    
    		If TypeOf (CType(pop.Parent, Grid).Parent) Is Page Then : pop.HorizontalOffset = CType(CType(pop.Parent, Grid).Parent, Page).ActualWidth / 2 + CType(pop.Parent, Grid).ActualWidth / 2 - childwidth
    		ElseIf TypeOf (CType(pop.Parent, Grid).Parent) Is Viewbox Then : pop.HorizontalOffset = CType(CType(pop.Parent, Grid).Parent, Viewbox).ActualWidth / 2 + CType(pop.Parent, Grid).ActualWidth / 2 - childwidth
    		End If
    	End If
    End Sub
    And the Exception occurs on the line where I attempt to assign the value to childwidth. The debugger shows the value of pop.Child.DesiredSize.Width as being 0.0 at this point (I would think this just gave childwidth a value of 0, but it gives an Exception instead). That Extension method is called from the following Extension method:
    <Extension()> Public Sub OffsetResizeSettingsPanel(pop As Popup)
    	If pop IsNot Nothing AndAlso pop.Child IsNot Nothing Then
    		If TypeOf (CType(pop.Parent, Grid).Parent) Is Page Then
    			If TypeOf (pop.Child) Is Grid Then : CType(pop.Child, Grid).Height = CType(CType(pop.Parent, Grid).Parent, Page).ActualHeight
    			ElseIf TypeOf (pop.Child) Is Border Then : CType(pop.Child, Border).Height = CType(CType(pop.Parent, Grid).Parent, Page).ActualHeight
    			End If
    		ElseIf TypeOf (CType(pop.Parent, Grid).Parent) Is Viewbox Then
    			If TypeOf (pop.Child) Is Grid Then : CType(pop.Child, Grid).Height = CType(CType(CType(pop.Parent, Grid).Parent, Viewbox).Parent, Page).ActualHeight
    			ElseIf TypeOf (pop.Child) Is Border Then : CType(pop.Child, Border).Height = CType(CType(CType(pop.Parent, Grid).Parent, Viewbox).Parent, Page).ActualHeight
    			End If
    		End If
    		pop.HorizontalOffsetRight()
    		pop.VerticalOffsetTop()
    	End If
    End Sub

    And that Extension method is called from a Button Click Handler using the following statement:
    Me.popSettings.OffsetResizeSettingsPanel()

    popSettings has the following XAML:
    <Popup x:Name="popSettings" Grid.ColumnSpan="3" IsLightDismissEnabled="True">
    	<Grid Style="{StaticResource SettingsGridStyle}">
    		<Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/><RowDefinition Height="Auto"/></Grid.RowDefinitions>
    		<Grid.ColumnDefinitions><ColumnDefinition Width="*"/></Grid.ColumnDefinitions>
    		<TextBlock Grid.Row="0" Style="{StaticResource SettingsHeaderTextBlockStyle}" Text="Player Names"/>
    		<ScrollViewer x:Name="scrPlayers" Grid.Row="1" HorizontalScrollMode="Disabled" HorizontalScrollBarVisibility="Hidden" VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" Margin="5,0,5,40">
    			<ctrl:PlayerNames x:Name="itmPlayerNames"/>
    		</ScrollViewer>
    		<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,3">
    			<Button x:Name="btnSettingsOk" Content="OK" Style="{StaticResource BasicButton}" Width="127"/>
    			<Button Content="Cancel" Style="{StaticResource BasicButton}" Width="127" Command="{StaticResource ClosePopup}" CommandParameter="{Binding ElementName=popSettings}"/>
    		</StackPanel>
    	</Grid>
    </Popup>

    The SettingsGridStyle resource used for the Grid sets the Width of the Grid to 400. Because popSettings is defined in the XAML and the code is not called until the Button is clicked, I would assume the Popup is ready (especially since I have used code almost identical to this in other projects). Is there something here that doesn't look right? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, May 01, 2015 4:33 PM
  • Hi Nathan,

    Sorry, still not reproducible.

    When I was trying to work with your code, I found  pop.VerticalOffsetTop() is missing.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 04, 2015 8:22 AM
    Moderator
  • VerticalOffsetTop() is another one of my Extension methods, I did not include the code for that in my post because the debugger never gets to that method (it stops inside HorizontalOffsetRight()).

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, May 04, 2015 2:40 PM