繁体   English   中英

Xamarin Forms设备方向

[英]Xamarin Forms device orientation

我正在使用Forms应用程序,我只是想知道是否有人可以帮助我进行设备定位和旋转。

目前基本上我有三行图像按钮。 我想做的是将设备旋转到横向时,所有按钮全部显示在一条水平线上,而不是3条垂直行。

最好的方法是什么? 我知道在Android中,您只是创建另一个xml布局文件并引用它,在Xamarin Forms和XAML中是否有类似的方法?

我将在下面提供我的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>

如果有人可以帮助,那就太好了。

表单还没有OnRotation事件-但是您可以使用OnSizeAllocated实现类似的功能

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

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

然后,您可以根据需要将StackLayout的方向从“水平”更改为“垂直”。

形式导向在这里更详细地讨论。

暂无
暂无

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

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