如何解决我从全屏webview视频页面返回的问题

时间:2019-04-16 11:53:49

标签: webview xamarin.android xamarin.ios

我有一个页面显示ListView个视频,这些视频是从网址中以WebView个单元格的形式显示的,

            <ListView HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="Transparent" Margin="15,0,15,15">
                                <controls:FullScreenEnabledWebView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="{Binding viewSource}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

其中controls:FullScreenEnabledWebView是这样的自定义渲染:

   public class FullScreenEnabledWebView : WebView
        {
            public event Action action = delegate { };
            public static readonly BindableProperty EnterFullScreenCommandProperty =
                BindableProperty.Create(
                    nameof(EnterFullScreenCommand),
                    typeof(ICommand),
                    typeof(FullScreenEnabledWebView));

            public static readonly BindableProperty ExitFullScreenCommandProperty =
                BindableProperty.Create(
                    nameof(ExitFullScreenCommand),
                    typeof(ICommand),
                    typeof(FullScreenEnabledWebView));

            public ICommand EnterFullScreenCommand
            {
                get => (ICommand)GetValue(EnterFullScreenCommandProperty); 
                set => SetValue(EnterFullScreenCommandProperty, value); 
            }


            public ICommand ExitFullScreenCommand
            {
                get => (ICommand)GetValue(ExitFullScreenCommandProperty);
                set => SetValue(ExitFullScreenCommandProperty, value);
            }

            public FullScreenEnabledWebView()
            {
                this.EnterFullScreenCommand = new Command<View>(DefaultEnterAsync);
                this.ExitFullScreenCommand = new Command(DefaultExitAsync);
            }


            private async void DefaultEnterAsync(View view)
            {
                var page = new FullScreenVideoPage()
                {

                    Content = view,
                };
                page.BackButtonPressed += () => { action.Invoke(); };
                await Application.Current.MainPage.Navigation.PushModalAsync(page);
            }

            private async void DefaultExitAsync()
            {
                await Application.Current.MainPage.Navigation.PopModalAsync();
            }
  }

和我的android代码:

[assembly: ExportRenderer(typeof(HBRS.Controls.FullScreenEnabledWebView),typeof(FullScreenEnabledWebViewRenderer))]
namespace HBRS.Droid.Renderers.Controls
{
    public class FullScreenEnabledWebViewRenderer : WebViewRenderer
    {
        private FullScreenEnabledWebView _webView;
        FullScreenEnabledWebChromeClient client;
        public FullScreenEnabledWebViewRenderer(Context context) : base(context)
        {
        }

        /// <inheritdoc/>
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
            _webView = (FullScreenEnabledWebView)e.NewElement;
            _webView.action += client.OnHideCustomView;
        }
        protected override FormsWebChromeClient GetFormsWebChromeClient()
        {
            client = new FullScreenEnabledWebChromeClient();
            client.EnterFullscreenRequested += OnEnterFullscreenRequested;
            client.ExitFullscreenRequested += OnExitFullscreenRequested;
            return client;
        }
        private void OnEnterFullscreenRequested(
            object sender,
            EnterFullScreenRequestedEventArgs eventArgs)
        {
            if (_webView.EnterFullScreenCommand != null && _webView.EnterFullScreenCommand.CanExecute(null))
            {
                _webView.EnterFullScreenCommand.Execute(eventArgs.View.ToView());
            }
        }
        private void OnExitFullscreenRequested(object sender, EventArgs eventArgs)
        {
            if (_webView.ExitFullScreenCommand != null && _webView.ExitFullScreenCommand.CanExecute(null))
            {
                _webView.ExitFullScreenCommand.Execute(null);
            }
        }
    }
}

其中FullScreenEnabledWebChromeClient是:

public class FullScreenEnabledWebChromeClient : FormsWebChromeClient
    {
        public event EventHandler<EnterFullScreenRequestedEventArgs> EnterFullscreenRequested;
        public event EventHandler ExitFullscreenRequested;
        public override void OnHideCustomView()
        {
            base.OnHideCustomView();
            ExitFullscreenRequested?.Invoke(this, EventArgs.Empty);
        }
        public override void OnShowCustomView(View view, ICustomViewCallback callback)
        {
            base.OnShowCustomView(view,callback);
            EnterFullscreenRequested?.Invoke(this, new EnterFullScreenRequestedEventArgs(view));
        }
        public override bool OnCreateWindow(WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
        {
            return base.OnCreateWindow(view, isDialog, isUserGesture, resultMsg);
        }
    }

除以下情况外,整个过程运行良好: 1.列表视图显示视频。 2.打开某些视频的全屏。 3.使用android设备的“后退”按钮返回,而不会在打开全屏之前更改其拥有的视频状态。 4。WebView将会是黑屏,我不知道为什么!!

但是当使用webview按钮关闭全屏时,它运行良好,并且当我至少一次更改视频的状态(暂停播放或反之亦然),然后通过使用android设备返回按钮返回时,工作还不错!! 有什么帮助吗?。

2 个答案:

答案 0 :(得分:0)

问题应该是单击后退按钮时,Webview视频也处于全屏状态。因此,单击后退按钮时需要退出全屏。可以将Android后退按钮改写为以下波纹:


...

[Pipeline] withDockerRegistry
Wrote authentication to /var/jenkins_home/.dockercfg
[Pipeline] {

[Pipeline] sh
+ docker build -t portfolio-app .

Sending build context to Docker daemon     41MB
...

Successfully built 5cd890711511
Successfully tagged example-app:latest
[Pipeline] dockerFingerprintFrom

[Pipeline] sh
+ docker tag example-app registry.exp/example-app:latest

[Pipeline] sh
+ docker push registry.exp/example-app:latest
The push refers to repository [registry.exp/example-app]

4dbf42572f81: Preparing
83fc1127b6ec: Preparing
31c9a8db3bb1: Preparing
487b885434b5: Preparing
d2968d4fb613: Preparing
8170f2d5c43d: Preparing
a464c54f93a9: Preparing
8170f2d5c43d: Waiting
a464c54f93a9: Waiting
no basic auth credentials

答案 1 :(得分:0)

只需要关注内容。

在FullScreenEnabledWebview类中,有这些乐趣:

private async void DefaultEnterAsync(View view)
{
    var page = new FullScreenVideoPage()
    {
        Content = view,
    };
    page.BackButtonPressed += () => { action.Invoke(); };
    await Application.Current.MainPage.Navigation.PushModalAsync(page);
}

只需对其进行修改即可:

private async void DefaultEnterAsync(View view)
{
    var page = new FullScreenVideoPage()
    {
        Content = view,
    };
    page.BackButtonPressed += () => { action.Invoke(); };
    await Application.Current.MainPage.Navigation.PushModalAsync(page);
    page.Content.Focus();
}

有关更多信息,请参见https://github.com/mhaggag/XFAndroidFullScreenWebView,并查看相应的问题(与同一存储库有关):https://github.com/mhaggag/XFAndroidFullScreenWebView/issues/3