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

如何在Xamarin.Forms应用中访问Reduce Motion系统设置

实现方案

完全可以实现,你可以通过Xamarin.Forms自带的DependencyService封装双端平台检测逻辑,无需引入额外第三方依赖。

步骤1:共享项目定义公共接口

在Xamarin.Forms共享项目中新增服务接口:

public interface IMotionService
{
    // 返回是否开启了减少动画功能
    bool IsReduceMotionEnabled();
}

步骤2:iOS平台实现

在iOS平台项目中添加对应实现:

[assembly: Dependency(typeof(MotionService_iOS))]
namespace 你的应用命名空间.iOS
{
    public class MotionService_iOS : IMotionService
    {
        public bool IsReduceMotionEnabled()
        {
            // 直接调用系统无障碍属性判断
            return UIAccessibility.IsReduceMotionEnabled;
        }
    }
}

步骤3:Android平台实现

Android系统的减少动画对应三个全局动画缩放配置,任意一项为0即视为开启减少动画:

[assembly: Dependency(typeof(MotionService_Android))]
namespace 你的应用命名空间.Android
{
    public class MotionService_Android : IMotionService
    {
        public bool IsReduceMotionEnabled()
        {
            var animatorScale = Android.Provider.Settings.Global.GetFloat(
                Android.App.Application.Context.ContentResolver,
                Android.Provider.Settings.Global.AnimatorDurationScale, 1f);
            var transitionScale = Android.Provider.Settings.Global.GetFloat(
                Android.App.Application.Context.ContentResolver,
                Android.Provider.Settings.Global.TransitionAnimationScale, 1f);
            var windowScale = Android.Provider.Settings.Global.GetFloat(
                Android.App.Application.Context.ContentResolver,
                Android.Provider.Settings.Global.WindowAnimationScale, 1f);
            
            return animatorScale == 0 || transitionScale == 0 || windowScale == 0;
        }
    }
}

步骤4:共享项目中调用

在需要判断的业务逻辑处直接调用即可:

var reduceMotionEnabled = DependencyService.Get<IMotionService>().IsReduceMotionEnabled();
if (!reduceMotionEnabled)
{
    // 仅未开启减少动画时执行动画逻辑
}

补充说明

  • 上述实现无需申请任何额外权限,兼容Xamarin.Forms支持的所有主流系统版本
  • 如果你后续升级到.NET MAUI,可以直接用内置的DeviceInfo.Current.IsReduceMotionEnabled属性,无需自行编写平台代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 03:54:03