使用类的静态字段中的颜色 Xamarin.Forms



假设我有一个静态类,如下所示

namespace MyNameSpace {
    static class MyClass {
        public static Color MyColor = Color.Red;
    }
}

我正在创建一个具有这些字段的类,以便在其他 c# 类中使用它们。是否可以在 xaml 中使用相同的静态它们。像这样的东西?

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:Cols="clr-namespace:MyNameSpace"
         BackgroundColor="{Cols:MyClass.MyColor}" />

还是使用静态资源的其他方式?

您可以使用 x:Static 标记扩展。

<ContentPage ... 
     BackgroundColor="{x:Static Cols:MyClass.MyColor}"..

x:Static可以访问以下内容之一:

  • 公共静态字段
  • 公共静态属性
  • 公共常量字段
  • 枚举成员。