简体   繁体   English

Xamarin Forms设备方向

[英]Xamarin Forms device orientation

Im working on a Forms app and Im just wondering if someone could help me with device orientation and rotation. 我正在使用Forms应用程序,我只是想知道是否有人可以帮助我进行设备定位和旋转。

Basically at the moment i have three rows of image buttons. 目前基本上我有三行图像按钮。 What i want to do is when the device is rotated to landscape, then all of the buttons will all be displayed on one horizontal line instead of 3 vertical rows. 我想做的是将设备旋转到横向时,所有按钮全部显示在一条水平线上,而不是3条垂直行。

What would be the best approach for this? 最好的方法是什么? I know in Android you just create another xml layout file and just reference that, is there a similar way to do that in Xamarin Forms and XAML? 我知道在Android中,您只是创建另一个xml布局文件并引用它,在Xamarin Forms和XAML中是否有类似的方法?

I will provide my MainPage.xaml below: 我将在下面提供我的MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"            x:Class="MyApp_Crossplatform.Views.MainPage"
             Title="App"
             BackgroundColor="White">
  <ContentPage.Content>
    <StackLayout Padding="10" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical">
      <!--Main container layout-->
      <StackLayout VerticalOptions="Fill" HorizontalOptions="Center" Orientation="Vertical">
        <!--Layout for each menu component-->
        <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
          <Image
            x:Name="btnSocial"
            Source="imgsocial.png"
            WidthRequest="100"
            HeightRequest="100"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"/>
          <Image
            x:Name="btnCareer"
            Source="imgcareer.png"
            WidthRequest="100"
            HeightRequest="100"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"/>
        </StackLayout>
        <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
          <Image
            x:Name="btnSchedule"
            Source="imgschedule.png"
            WidthRequest="100"
            HeightRequest="100"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"/>
          <Image
            x:Name="btnContact"
            Source="contact.png"
            WidthRequest="100"
            HeightRequest="100"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"/>
        </StackLayout>
        <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
          <Image
            x:Name="btnDetails"
            Source="imgdetails.png"
            WidthRequest="100"
            HeightRequest="100"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"/>
        </StackLayout>
      </StackLayout>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

If anyone can help that would be great. 如果有人可以帮助,那就太好了。

Forms doesn't have a OnRotation event (yet) - but you can achieve something similar by using OnSizeAllocated 表单还没有OnRotation事件-但是您可以使用OnSizeAllocated实现类似的功能

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height); //must be called

    if (width > height) {
      // landscape
    } else {
      // portrait
    }
} 

you could then change the orientation of your StackLayout from Horizontal to Vertical as needed. 然后,您可以根据需要将StackLayout的方向从“水平”更改为“垂直”。

Orientation in Forms is discussed in more detail here . 形式导向在这里更详细地讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM