Search code examples
c#xamlgriduniversalsetvalue

crash on SetValue for grids wp8.1 universal


im trying to design tiles in one section of the pivot with 6 tiles both portrait and landscape. i want 3x2 (3rows) tiles for portrait and 2x3 tiles for landscape. it shows 3x2 in both modes without c# code but after adding that code it crashes on first Setvalue.

my xaml code:

 <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="172"/>
                            <RowDefinition Height="172"/>
                            <RowDefinition x:Name="more_3rdrow" Height="172"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="172"/>
                            <ColumnDefinition Width="172"/>
                            <ColumnDefinition x:Name="more_3rdcolumn" Width="0"/>
                    </Grid.ColumnDefinitions>
                      <Grid Grid.Row="0" Grid.Column="0" x:Name="contacttile">
                         some data here
                      </Grid>
                      <Grid Grid.Row="0" Grid.Column="1" x:Name="ratetile">
                         some data here
                      </Grid>
                      <Grid Grid.Row="1" Grid.Column="0" x:Name="moreappstile">
                         some data here
                        </Grid>

                      more grids here
</Grid>

my c# code:

     private void pivot_Loaded(object sender, RoutedEventArgs e)
   {
       moresectionLoaded = 1;
   }
     private void PageBase_SizeChanged(object sender, SizeChangedEventArgs e)
   {

       if (moresectionLoaded == 1)
       {


       string currentviewstate = ApplicationView.GetForCurrentView().Orientation.ToString();
       if (currentviewstate == "Portrait")
       {
           ///crashes in below line
           more_3rdrow.SetValue(RowDefinition.HeightProperty, 172);
           more_3rdcolumn.SetValue(ColumnDefinition.WidthProperty, 0);

           moreappstile.SetValue(Grid.RowProperty, 1);
           moreappstile.SetValue(Grid.ColumnProperty, 0);

           abouttile.SetValue(Grid.RowProperty, 1);
           abouttile.SetValue(Grid.ColumnProperty, 1);

           pintile.SetValue(Grid.RowProperty, 2);
           pintile.SetValue(Grid.ColumnProperty, 0);

           pintypestile.SetValue(Grid.RowProperty, 2);
           pintypestile.SetValue(Grid.ColumnProperty, 1);
       }
       if (currentviewstate == "Landscape")
       {

           ///crashes in below line
           more_3rdrow.SetValue(RowDefinition.HeightProperty, 0);
           more_3rdcolumn.SetValue(ColumnDefinition.WidthProperty, 172);

           moreappstile.SetValue(Grid.RowProperty, 0);
           moreappstile.SetValue(Grid.ColumnProperty, 2);

           abouttile.SetValue(Grid.RowProperty, 1);
           abouttile.SetValue(Grid.ColumnProperty, 0);

           pintile.SetValue(Grid.RowProperty, 1);
           pintile.SetValue(Grid.ColumnProperty, 1);

           pintypestile.SetValue(Grid.RowProperty, 1);
           pintypestile.SetValue(Grid.ColumnProperty, 2);
       }
       }
   }

Solution

  • The Height in the RowDefinition DependendyObject is not double is GridLenght class, so you have to use:

    more_3rdrow.SetValue(RowDefinition.HeightProperty, new GridLength(172));
    

    This is because you can define Auto, 1*, or fixed size. So be careful with ColumDefinition too