You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms中特定页面(视频页)屏幕旋转配置问题

解决Xamarin.Forms部分页面支持横屏的方案

你之前在MainActivity上设置了ScreenOrientation = ScreenOrientation.Portrait,这会全局锁定所有页面的竖屏状态,所以得先调整这个配置,再通过动态控制实现部分页面的横屏支持。下面是具体的实现步骤:

步骤1:修改Android端MainActivity的配置

先把MainActivity的Activity属性里的ScreenOrientation = ScreenOrientation.Portrait去掉,让系统允许动态切换方向。修改后的属性代码如下:

[Activity(Label = "PG_ELearning.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    // ... 原有其他代码
}

步骤2:在共享代码中实现方向控制逻辑

这里提供两种常用的实现方式,选一种你觉得顺手的就行:

方式一:使用MessagingCenter传递状态消息

  1. 在共享代码中定义一个统一的消息标识类,方便管理:
public static class OrientationControlMessages
{
    public const string UpdateOrientation = "UpdateOrientation";
}
  1. 在你的视频播放页面中,重写页面生命周期方法,发送方向切换的消息:
protected override void OnAppearing()
{
    base.OnAppearing();
    // 进入页面时允许横屏(支持传感器自动切换横竖屏)
    MessagingCenter.Send(this, OrientationControlMessages.UpdateOrientation, true);
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    // 离开页面时锁定回竖屏
    MessagingCenter.Send(this, OrientationControlMessages.UpdateOrientation, false);
}
  1. 回到Android端的MainActivity,在OnCreate方法中订阅消息,动态设置屏幕方向:
protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(bundle);
    global::Xamarin.Forms.Forms.Init(this, bundle);

    // 应用启动时默认锁定竖屏
    RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;

    // 订阅方向切换消息
    MessagingCenter.Subscribe<object, bool>(this, OrientationControlMessages.UpdateOrientation, (sender, allowLandscape) =>
    {
        if (allowLandscape)
        {
            // 允许传感器检测方向,支持横竖屏自动切换
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Sensor;
        }
        else
        {
            // 锁定为竖屏
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
        }
    });

    LoadApplication(new App());
}

方式二:使用依赖服务(DependencyService)

  1. 在共享代码中定义方向控制的接口:
public interface IOrientationService
{
    // 允许横屏(传感器自动切换)
    void EnableLandscapeMode();
    // 锁定为竖屏
    void LockPortraitMode();
}
  1. 在Android端实现这个接口:
[assembly: Dependency(typeof(AndroidOrientationService))]
namespace PG_ELearning.Droid
{
    public class AndroidOrientationService : IOrientationService
    {
        public void EnableLandscapeMode()
        {
            var currentActivity = global::Xamarin.Forms.Forms.Context as Activity;
            currentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Sensor;
        }

        public void LockPortraitMode()
        {
            var currentActivity = global::Xamarin.Forms.Forms.Context as Activity;
            currentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
        }
    }
}
  1. 在视频播放页面中调用依赖服务:
protected override void OnAppearing()
{
    base.OnAppearing();
    DependencyService.Get<IOrientationService>().EnableLandscapeMode();
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    DependencyService.Get<IOrientationService>().LockPortraitMode();
}

注意事项

  • 确保MainActivity的ConfigurationChanges已经包含ConfigChanges.ScreenSize | ConfigChanges.Orientation,这样屏幕旋转时不会重启Activity,避免页面状态丢失。
  • 如果需要视频页面强制横屏而非自动切换,可以把ScreenOrientation.Sensor换成ScreenOrientation.Landscape,不过传感器模式会更符合用户操作习惯。

内容的提问来源于stack exchange,提问作者pallav bohara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:30:23