c# - 从 Xamarin 中的代码推送新页面后如何旋转屏幕

标签 c# xamarin xamarin.forms youtube youtube-api

标题说得差不多了,我正在推送一个包含 VideoSource 的 WebView
女巫有一个 youtube 视频嵌入 HTML 代码。
一旦它被插入,我需要强制它进入全屏模式。
这是我正在使用的一段代码,如果您有任何其他/更好的方法,请回答

我是新手所以请放轻松:)

我在这个主题上搜索了一下,但无论如何我都找不到在插入屏幕时仅在 1 页上旋转屏幕。

    public partial class MainScreen : TabbedPage
    {
        public HtmlWebViewSource VideoSource { get; set; }

        private const string PlayerHTML = @"<html>
             <body>  
             <iframe style='position: absolute; top: 0; left: 0; width: 100%; height: 100%;'  
             src='https://www.youtube.com/embed/6e7qKcVigQo?rel=0?autoplay=1?modestbranding=1' 
             frameborder='0' 
             allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' 
             allowfullscreen>
             </iframe>
             </body>
             </html>";

        private async void ImageButton_Clicked(object sender, EventArgs e)
        {
            VideoSource = new HtmlWebViewSource
            {
                Html = PlayerHTML
            };

            var a = new ContentPage
            {
                Content = new WebView()
                {

                    Source = VideoSource
                }
            };
            NavigationPage.SetHasNavigationBar(a, false);
            await Navigation.PushAsync(a);
            //make the screen rotate SOMEHOW
        }
    }


我期望有两种解决方案;
  • Xamarin 相关解决方案,例如通过到达 MainActivity.cs
  • 来旋转屏幕
  • HTML/YouTube 嵌入解决方案,例如视频链接中的内容,可强制全屏播放视频...
  • 最佳答案

    您可以使用消息中心来处理
    在安卓上:

       protected override void OnCreate(Bundle savedInstanceState)
        {
            Current = this;
            //TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            base.OnCreate(savedInstanceState);
            Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
            //allowing the device to change the screen orientation based on the rotation
            MessagingCenter.Subscribe<VideoPlayerView>(this, "allowLandScape", sender =>
            {
                RequestedOrientation = ScreenOrientation.Landscape;
            });
    
            //during page close setting back to portrait
            MessagingCenter.Subscribe<VideoPlayerView>(this, "preventLandScape", sender =>
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            });
    
            LoadApplication(new App());
        }
    

    叫那个
       protected override void OnAppearing()
        {
            base.OnAppearing();
            MessagingCenter.Send(this, "allowLandScape");
    
        }
    
        //during page close setting back to portrait
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            MessagingCenter.Send(this, "preventLandScape");
    
        }
    

    关于c# - 从 Xamarin 中的代码推送新页面后如何旋转屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57732259/

    相关文章:

    c# - DDD : Where should I set modified date and modified by? 存储库还是应用程序服务?

    c# - 如果我指定了确切的结构,为什么 TryParseExact 需要 CultureInfo?

    c# - LINQ to Entities 无法识别方法组连接

    visual-studio - 启动应用程序时出错 : Failed to forward ports

    C# 安卓 : Get Broadcast Receiver on a service?

    c# - 在 Xamarin Studio 中以编程方式创建 NSView

    c# - 如何在 ASP.NET 中获取当前域名

    ios - Xamarin iOS 工具栏背景图片

    Xamarin 包安装错误

    iOS 应用程序加载器 : ERROR ITMS-90705: "Launch storyboard not found..."