Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 81910

Problem while back from one page with horizontal orientation to another page vertical orientation

$
0
0

When I firstly show MainPage it shows properly all and in right orientation. I go to SecondPage, in this page also all is fine. But when I return to previous page( MainPage) orientation doesn't change.

Page 1:

    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 for Page 1:

<?xml version="1.0" encoding="utf-8" ?>
<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>

Page 2:

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()));
        }
    }

MainActivity:

 [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;
            });
        }
    }

Viewing all articles
Browse latest Browse all 81910

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>