如何将另一个类的属性值设置为XAML属性

时间:2019-07-16 12:40:51

标签: c# xamarin binding

我正在尝试将另一个类的属性值设置为XAML UI元素property。 我有XAML和称为“ Config”的静态类。 在Config类中,我有公共静态类Theme。 在主题类中,我具有属性primaryColor。 因此,我需要将primaryColor设置为XAML中的UI元素。

我尝试了x:Static,但是它对我不起作用,因为Theme类中的字段不是静态的。

XAML:

<StackLayout BackgroundColor={x:Static config:Config.CurrentTheme.primaryColor}></StackLayout>

Config.cs:

public static class Config
{
    public static Theme CurrentTheme { get; set; }
}

Theme.cs:

public class Theme
{
    public Color primaryColor { get; set; } = Color.FromHex("#1D1E1F");
    public Color secondaryColor { get; set; } = Color.FromHex("#252625");
    public Color grayColor { get; set; } = Color.FromHex("#2F2F2F");
    public Color lightGrayColor { get; set; } = Color.FromHex("#626261");
    public Color goldColor { get; set; } = Color.FromHex("#CAA440");
    public Color lightGreenColor { get; set; } = Color.FromHex("#28A745");
    public Color darkRedColor { get; set; } = Color.FromHex("#F0373A");
}

2 个答案:

答案 0 :(得分:0)

当您阅读有关x:Static的内容时,它会直接说:

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

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

由于您的媒体资源不符合上述条件,因此无效!

答案 1 :(得分:0)

静态添加到 Theme.cs ,然后可以在Xaml中使用:

public static class Theme
{
    public static Color primaryColor { get; set; } = Color.FromHex("#1D1E1F");
    public static Color secondaryColor { get; set; } = Color.FromHex("#252625");
    public static Color grayColor { get; set; } = Color.FromHex("#2F2F2F");
    public static Color lightGrayColor { get; set; } = Color.FromHex("#626261");
    public static Color goldColor { get; set; } = Color.FromHex("#CAA440");
    public static Color lightGreenColor { get; set; } = Color.FromHex("#28A745");
    public static Color darkRedColor { get; set; } = Color.FromHex("#F0373A");
}

Xaml

<StackLayout BackgroundColor="{x:Static local:Theme.grayColor}"></StackLayout>

别忘了在xaml中添加本地参考:

xmlns:local="clr-namespace:YourprojectNameSpace"

==================================更新============ =======================

如果要通过viewmodel更改具有绑定属性的颜色,请尝试使用IValueConverter

public class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string valueAsString = value.ToString();
        switch (valueAsString)
        {
            case (""):
                {
                    return Color.Default;
                }
            case ("Accent"):
                {
                    return Color.Accent;
                }
            default:
                {
                    return Color.FromHex(value.ToString());
                }
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

Config.cs应作如下修改:

public class Config : INotifyPropertyChanged
{
    private string mycolor;

    public string MyColor
    {
        get { return mycolor; }
        set
        {
            mycolor = value;
            OnPropertyChanged("MyColor");
        }
    }

    public Config (){
        mycolor = "#00FF00"; // can set a defalut color here
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

在Xaml中:

<ContentPage.Resources>
   <ResourceDictionary>
        <local:StringToColorConverter x:Key="StringToColorConverter"/>
   </ResourceDictionary>
</ContentPage.Resources>

<StackLayout BackgroundColor="{Binding MyColor, Converter={StaticResource StringToColorConverter}}"></StackLayout>

最后,ContentPage需要绑定模型Config.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        Config config = new Config();
        this.BindingContext = config;

        //MyColor can be modified runtime
          config.MyColor = "#00FF00";
    }
}