iOS 上的 VisualElement 旧版颜色模式

一些 Xamarin.Forms 视图具有旧版颜色模式。 在此模式下,当视图的 IsEnabled 属性设置为 false 时,视图将使用默认本机颜色替代用户设置的处于禁用状态的颜色。 为了实现向后兼容,这种旧版颜色模式仍然是受支持视图的默认行为。

此项 iOS 平台特定功能禁用了 VisualElement 上的旧版颜色模式,以便在视图处于禁用状态时,用户对视图设置的颜色仍然可以保留。 其使用方式为,在 XAML 中将 VisualElement.IsLegacyColorModeEnabled 附加属性设置为 false

<ContentPage ...
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        ...
        <Button Text="Button"
                TextColor="Blue"
                BackgroundColor="Bisque"
                ios:VisualElement.IsLegacyColorModeEnabled="False" />
        ...
    </StackLayout>
</ContentPage>

或者,可以使用 Fluent API 从 C# 使用它:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
...

_legacyColorModeDisabledButton.On<iOS>().SetIsLegacyColorModeEnabled(false);

VisualElement.On<iOS> 方法指定此平台特定仅在 iOS 上运行。 命名空间 Xamarin.Forms.PlatformConfiguration.iOSSpecific 中的 VisualElement.SetIsLegacyColorModeEnabled 方法用于控制是否禁用旧版颜色模式。 此外,VisualElement.GetIsLegacyColorModeEnabled 方法还可用于返回是否禁用了旧版颜色模式。

结果是可以禁用旧版颜色模式,以便即使禁用视图,用户对视图设置的颜色也会保留:

已禁用旧颜色模式

注意

在视图上设置 VisualStateGroup 时,将完全忽略旧版颜色模式。 若要详细了解视觉对象状态,请参阅 Xamarin.Forms 视觉对象状态管理器