How can I change the fixed value `WidthRequest =" 250 "` just to be used in portrait mode?

2021-02-11T13:10:53.897+00:00

In landscape mode I want the Label to occupy the entire line.

I'm using the MVVM architecture.

<StackLayout Orientation="Horizontal"
                       VerticalOptions="CenterAndExpand"
                       HorizontalOptions="FillAndExpand">
       <Label Text="{Binding app_nome}" 
                   Style="{StaticResource LabelStyle}" 
                   FontSize="20" 
                   FontAttributes="Bold"
                   VerticalTextAlignment="Start"
                   WidthRequest="250"
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="StartAndExpand"/>

       <Label Text="{Binding dt_cadastro_hora}"
                   Style="{StaticResource LabelStyle}" 
                   FontSize="13"
                   FontAttributes="Bold"
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="End"
                   LineBreakMode="NoWrap"/>
</StackLayout>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-02-12T03:13:04.51+00:00

    Hello,

    Welcome to Microsoft Q&A!

    • First select and enable device orientation in each platform .

    iOS : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=windows#ios
    Android : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=windows#android
    UWP : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=windows#universal-windows-platform

    • We can use OnSizeAllocated method to react to changes in orientation , the method is called whenever a Page is allocated a new size, which happens whenever the device is rotated. protected override void OnSizeAllocated (double width, double height){
      base.OnSizeAllocated (width, height);
      if (width != this.width || height != this.height) {
      this.width = width;
      this.height = height;
      if (width > height) {
      label.WidthRequest = 400;
      } else {
      label.WidthRequest = 250;
      }
      }
      }

    67195-captur2e.png
    67232-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments