如何在 Xamarin 中为常量分配颜色?

问题描述

这是我想要做的:

public static partial class Const
{

    public static class IconColors
    {
        public const Color  IconBlue1 = Color.FromHex("007AFF");
    }

}

但是它给了我一个错误,说颜色类型不能被声明为 const。

有人有什么建议/建议吗?

解决方法

你可以像下面这样定义

public class IconColors
{
        public static Color IconBlue1 { get; } = Color.FromHex("007AFF");
}

并像访问它一样

var color = IconColors.IconBlue1;