从一个水平方向的页面返回到另一个垂直方向的页面时出现问题

Issue when come back from one page with horizontal orientation to another page with vertical

首先显示 MainPage,然后我单击按钮并调用 SecondPage。一切看起来都很好,但是当我点击后退按钮时(并且显示的方向应该是纵向的,但是我得到一些奇怪的行为,比如纵向(水平)视图的纵向。

Page1(右垂直方向)-> Page2(右水平方向)->(后退按钮操作) -> Page1(垂直方向错误)。

   when firstly showing                         when click back button

主页:

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
        {
            var fullScreenVideoPage = new SecondPage();
            NavigationPage.SetHasNavigationBar(fullScreenVideoPage, false);
            await Navigation.PushAsync(fullScreenVideoPage);
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            MessagingCenter.Send(this, "preventLandScape");
        }
    }

XAML(主页):

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage"
             BackgroundColor="Black">
      <StackLayout x:Name="stackLayout" Orientation="Horizontal">
            <Image Source="icon.png"               
                   WidthRequest="150"
                   HeightRequest="150"
                   HorizontalOptions ="CenterAndExpand"
                   VerticalOptions="CenterAndExpand">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer
                        Tapped="OnTapGestureRecognizerTapped"  />
                </Image.GestureRecognizers>
            </Image>     
            </StackLayout>
</ContentPage>

第二页:

public partial class SecondPage : ContentPage
    {
        public SecondPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            MessagingCenter.Send(this, "showLandscapeOrientation");
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            MessagingCenter.Send(this, "showPortraitOrientation");
        }

        async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
        {
            await Navigation.PushAsync(new NavigationPage(new MainPage()));
        }
    }

主要活动:

[Activity(Label = "App1", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication(new App());
            //allowing the device to change the screen orientation based on the rotation
            MessagingCenter.Subscribe<SecondPage>(this, "showLandscapeOrientation", sender =>
            {
                RequestedOrientation = ScreenOrientation.Landscape;
            });

            //during page close setting back to portrait
            MessagingCenter.Subscribe<SecondPage>(this, "preventLandScape", sender =>
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            });

            MessagingCenter.Subscribe<MainPage>(this, "showPortraitOrientation", sender =>
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            });
        }
    }

问题是您为 MainPage 订阅了 showPortraitOrientation,但您从 MainPage 调用了 preventLandScape

所以你可以这样修改MainPageOnAppearing()中的代码:

protected override void OnAppearing()
{
    base.OnAppearing();
    MessagingCenter.Send(this, "showPortraitOrientation");
}

其他问题是,当从横向切换回纵向屏幕时,您可能需要重新绘制布局。对于 Android 平台,您可以像这样编写代码:

protected override void OnAppearing()
{
    base.OnAppearing();
    MessagingCenter.Send(this, "showPortraitOrientation");

    //force redraw
    this.InvalidateMeasure();
}

protected override void InvalidateMeasure()
{
    Task.Delay(200).Wait();
    base.InvalidateMeasure();
}