locked
Calculating Image Height During OrientationChanged

    Question

  • I have a ViewBox, Image, and TextBlock. When the OrientationChanged event occurs, I want to set the height of the ViewBox using the following:
    Me.vbxSelected.Height = Me.imgIsSet.ActualHeight - Me.txtIsSet.ActualHeight
    However, because the OrientationChanged event occurs before the Loaded event, these values are 0 at this point. So I tried using the Measure() method to get the DesiredSize property. This worked fine for the TextBlock, but the Image was still 0. The Source for my Image is an ImageSource in the Resources from another project. The Image and this ImageSource work fine during runtime, but I still cannot get the height for the Image at this point. I also tried this in the Loaded event, but the Image was still 0. Where can I get or what can I do to calculate the height when my app first loads? Thanks.

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

    Thursday, October 30, 2014 4:32 AM

Answers

  • I don't know if it's the best solution, but I tried doing it in the LayoutUpdated event as follows:
    Private Sub MainPage_LayoutUpdated(sender As Object, e As Object) Handles Me.LayoutUpdated
    	If Me.imgIsSet.ActualHeight <> 0 AndAlso Me.txtIsSet.ActualHeight <> 0 Then
    		If DisplayInformation.GetForCurrentView().CurrentOrientation = DisplayOrientations.Landscape OrElse DisplayInformation.GetForCurrentView().CurrentOrientation = DisplayOrientations.LandscapeFlipped Then
    			Me.vbxSelected.Height = Me.imgIsSet.ActualHeight - Me.txtIsSet.ActualHeight
    		Else : Me.vbxSelected.Height = Double.NaN
    		End If
    	End If
    End Sub
    This event gets called multiple times, so I don't know if it is the most efficient, but it works for now. Hopefully this can help others as well, and if anybody has any suggestions as to what might be a better solution, I welcome other suggestions as well.

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

    Thursday, October 30, 2014 4:59 PM