locked
Rerendering after OrientationChanged

    Question

  • I have a Windows 8.1 app for which I want to support multiple orientations. I have added an event handler for the OrientationChanged event as follows:
    AddHandler DisplayInformation.GetForCurrentView().OrientationChanged, AddressOf Me.DisplayInformation_OrientationChanged
    In this handler, I modify my main Grid to rearrange a couple elements in my layout (for example, so that I can display Buttons below or beside other stuff, depending). The rearranging seems to work fine, since I can see that my elements switch between above/below to beside each other, and vice-versa. But what does not happen is the resizing of the elements. It basically looks to me like the elements are the same size, just rearranged. The initial Grid.RowDefinitions and Grid.ColumnDefinitions in my XAML are:
    <Grid.RowDefinitions>
    	<RowDefinition Height="Auto"/>
    	<RowDefinition Height="*"/>
    	<RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    	<ColumnDefinition Width="Auto"/>
    	<ColumnDefinition Width="*"/>
    	<ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    And in my DisplayInformation_OrientationChanged handler, I change them to:
    <Grid.RowDefinitions>
    	<RowDefinition Height="Auto"/>
    	<RowDefinition Height="*"/>
    	<RowDefinition Height="Auto"/>
    	<RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    	<ColumnDefinition Width="Auto"/>
    	<ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    Do I need to somehow tell it to recalculate the widths & heights? It seems like it is always calculating the size of the elements for landscape (regardless of which orientation it is in) and then (as I tell it to in my OrientationChanged handler) rearranging them without recalculating the size for the new orientation. Manually recalculating the sizes seems like a lot of unnecessary work for something like this. What should I do? Thanks.

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

    Thursday, October 2, 2014 1:57 AM

Answers

  • The effort & frustration we sometimes put ourselves through because of a stupid little thing we miss… In my code, I was setting the second column to Auto instead of * when switching to portrait, therefore allowing the Scrollviewer to make itself wider than the screen, and then I was setting it back to * when switching back to landscape, making landscape look good but portrait bad. I shouldn't have been changing that one at all, since it is * in both orientations (I must have forgotten to remove it after previous design modifications)! So problem solved! Thank you for your help (and patience) anyway!

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

    Thursday, October 2, 2014 4:53 PM

All replies

  • how are your controls or items inside of the grids Aligned (as in the vertical/horizontal align properties in visual studio)? i found for my app that if i left the horizontal and vertical alignment properties to 'stretch' and resized the grid Definitions using stars '*' instead of pixels  when the phone orientation changes i didn't have to do much if anything as far as resizing because the star definition is basically a % of the screen, which shouldn't change with orientation.

    do you have the xaml of a button or control you can post?

    Just a though.

    Thursday, October 2, 2014 2:26 AM
  • My controls are several ScrollViewers (none of which have any of the align properties set) and a Grid (for which I set both VerticalAlign and HorizontalAlign to Center). I tried removing the align properties from the Grid, but it did not make any difference. The ScrollViewers all have ItemsControls in them, and the Grid is just a bunch of Buttons. The control not being resized is one of the ScrollViewers (it is the control that is in row=1,column=1, so it is in a cell with a * for both width and height). I have heard that in some cases ScrollViewers do not always resize themselves the way you want if you do not set an explicit width or height, but in order to do that, I would need to manually calculate the size, which is what I want to avoid. Any ideas? Thanks.

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

    Thursday, October 2, 2014 2:47 AM
  • I haven't worked a lot with scrollviewers in a grid layout like that. I do have a lot of problems with the layout in visual studio not doing what i would suspect it to do when the program is compiled (probably operator error on my behalf). In any case i would suggest trying diffrent ways of layout your scrollviewers. for example if its possible  put your scroll viewer inside of a Grid with the grid being the control that is set to Stretch horizontal alignment (also being the outer most control).

    Something like this.

    <Grid> <ScrollViewer>

    <!--controls / items here --> </ScrollViewer> </Grid>


    Thursday, October 2, 2014 3:04 AM
  • The effort & frustration we sometimes put ourselves through because of a stupid little thing we miss… In my code, I was setting the second column to Auto instead of * when switching to portrait, therefore allowing the Scrollviewer to make itself wider than the screen, and then I was setting it back to * when switching back to landscape, making landscape look good but portrait bad. I shouldn't have been changing that one at all, since it is * in both orientations (I must have forgotten to remove it after previous design modifications)! So problem solved! Thank you for your help (and patience) anyway!

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

    Thursday, October 2, 2014 4:53 PM
  • No problem! good luck and have fun coding !
    Thursday, October 2, 2014 8:17 PM