How do I use my Code Behind in combination with my xaml

Hi Guys,

well this question is maybe ridiculous but I have some problems there.
I have an xaml like this:

<?xml version="1.0" encoding="utf-8" ?>

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:translation="clr-namespace:CSScan.Resources;assembly=CSScan"

         x:Class="CSScan.Prozesse.Abstract.CListWorkStep.Widgets.View.PickPeekWidgetView"
         xmlns:converters="clr-namespace:CSScan.Util;assembly=CSScan"         
         x:Name="ultra"
         VerticalOptions="End">

  <Grid.ColumnDefinitions>
  <ColumnDefinition Width="Auto"/>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="*"/>       
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
  <RowDefinition Height="*"/>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="*"/>
</Grid.RowDefinitions>


  <Label x:Name="lblHead" Text="Übersicht nächste Position: " FontSize="20" FontAttributes="Bold" HorizontalOptions="CenterAndExpand" Grid.Column="0" Grid.Row="1"/>
  <Label x:Name="lblT_artNr" Text="Artikelnummer: " FontSize="30" FontAttributes="Italic" HorizontalOptions="CenterAndExpand" BackgroundColor="Gray" Grid.Column="1" Grid.Row="1"/>
  <Label Text="{Binding Artikelnr}" x:Name="lblArtikelnr" FontSize="30" FontAttributes="Bold" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Grid.Column="1" Grid.Row="2"/>

  <Label x:Name="lblT_menge" Text="Menge:" FontSize="30" FontAttributes="Italic" HorizontalOptions="CenterAndExpand" Grid.Column="2" Grid.Row="1" BackgroundColor="Gray"/>
  <Label Text="{Binding Menge}" x:Name="lblMenge" FontSize="30" FontAttributes="Bold" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Grid.Column="2" Grid.Row="2"/>

  <Label x:Name="lblT_lagerort" Text="Lagerort: " FontSize="30" FontAttributes="Italic" HorizontalOptions="CenterAndExpand" Grid.Column="3" Grid.Row="1" BackgroundColor="Gray"/>
  <Label Text="{Binding Lagerort}" x:Name="lblLagerort" FontSize="30" FontAttributes="Bold" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Grid.Column="3" Grid.Row="2"/>

  <Label x:Name="lblT_kurztext" Text="Kurztext: " FontSize="30" FontAttributes="Italic" HorizontalOptions="CenterAndExpand" Grid.Column="4" Grid.Row="1" BackgroundColor="Gray"/>
  <Label Text="{Binding Kurztxt}" x:Name="lblKurztxt" FontSize="30" FontAttributes="Bold" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Grid.Column="4" Grid.Row="2"/>

I use my Stacklayout as my content because I want to attach my xaml in another xaml. But the thing is, when I use my code behind to change the size of one of my label (for example after changing the height and width of my simulator) then nothing happens.

Example Code:
public void checkChange(double height, double width)
{
if (Device.Idiom == TargetIdiom.Phone)
{

        }
        if (Device.Idiom == TargetIdiom.Desktop || Device.Idiom == TargetIdiom.Tablet)
        {

            this.gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
            this.gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            this.gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            this.gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

            this.gridLayout.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            this.gridLayout.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            this.gridLayout.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

            this.lblHead.FontSize = 20;
            this.lblHead.FontAttributes = FontAttributes.Bold;
            this.lblHead.HorizontalOptions = LayoutOptions.CenterAndExpand;
            this.lblHead.Text = "TEST";

            this.lblT_artNr.FontSize = 30;
            this.lblT_artNr.FontAttributes = FontAttributes.Italic;
            this.lblT_artNr.HorizontalOptions = LayoutOptions.CenterAndExpand;
            this.lblT_artNr.BackgroundColor = Color.Gray;
            this.lblArtikelnr.FontSize = 30;
            this.lblArtikelnr.FontAttributes = FontAttributes.Bold;
            this.lblArtikelnr.HorizontalOptions = LayoutOptions.CenterAndExpand;
            this.lblArtikelnr.VerticalOptions = LayoutOptions.Center;

Well this code behind doesn't work and I don't know why. Maybe I figured out that on a content page I say in a code -- Content = gridLayout; -- to change my Content. But in my xaml it's not possible because I want to use StackLayout at first and not ContentPage. Maybe someone know what I have to do?

Best greetings
Lam Son

Sign In or Register to comment.