简体   繁体   中英

Webview display problem on Android when changing page and display mode (Portrait -> Landscape)

when I try to display in a webView one of my web page in Landscape mode ( https://web-labosims.org/animations/App_refraction_reflexion/App_refraction&reflexion.html ) it works correctly on Android.

Screenshots

But when it's not the first page and I change orientation between pages it doesn't work properly anymore on Android (display problem)...

MainPage.xaml.cs

    public partial class MainPage : ContentPage
    {
        private List<Anim> lesAnimations;
        public MainPage()
        {
            InitializeComponent();

            String animsJson = "[{\"Nom\":\"Réfraction et réfexion\",\"url\":\"https://web-labosims.org/animations/App_refraction_reflexion/App_refraction&reflexion.html\",\"imageUrl\":\"App_refraction.png\"},{\"Nom\":\"Synthèse soustractive\",\"url\":\"https://web-labosims.org/animations/App_lumiere3/App_lumiere3.html\",\"imageUrl\":\"App_soustractive.png\"}]";

            lesAnimations = JsonConvert.DeserializeObject<List<Anim>>(animsJson);

            maListView.ItemsSource = lesAnimations;

            maListView.ItemSelected += (sender, e) =>
            {
                if (maListView.SelectedItem != null)
                {
                    Anim item = maListView.SelectedItem as Anim;

                    GoAnimation(item.url);
                }
                maListView.SelectedItem = null;
            };

        }

        private async Task GoAnimation(String url)
        {
            await this.Navigation.PushAsync(new Animation(url));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            DependencyService.Get<IOrientationHandler>().ForcePortrait();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            DependencyService.Get<IOrientationHandler>().ForceLandscape();
        }

    }

Animation.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:LaboSims;assembly=LaboSims"
             x:Class="LaboSims.Animation">
    <ContentPage.Content>
            <WebView x:Name="maWebView"  
                     WidthRequest="1000"
                     HeightRequest="1000"
                     />
    </ContentPage.Content>
</ContentPage>

Animation.xaml.cs

public partial class Animation : ContentPage
    {
        public Animation(String url)
        {
            InitializeComponent();

            NavigationPage.SetHasNavigationBar(this, false);

            maWebView.Source = url;
        }

    }

IOrientationHandler.cs

namespace LaboSims
{
    public interface IOrientationHandler
    {
        void ForceLandscape();
        void ForcePortrait();
    }
}

And on Android

using Android.Content.PM;
using LaboSims.Droid;
using Plugin.CurrentActivity;

[assembly: Xamarin.Forms.Dependency(typeof(OrientationHandler))]
namespace LaboSims.Droid
{
    public class OrientationHandler : IOrientationHandler
    {
        public void ForceLandscape()
        {
            CrossCurrentActivity.Current.Activity.RequestedOrientation = ScreenOrientation.Landscape;
        }

        public void ForcePortrait()
        {
            CrossCurrentActivity.Current.Activity.RequestedOrientation = ScreenOrientation.Portrait;
        }
    }
}

Screenshots

I don't understand where I make an error...

I was sure that the problem should be simple and indeed it is a simple mistake... I had made a small mistake in my meta tag on my web page. I changed it to:

<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">

And everything is working properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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