【问题标题】:Problem changing background color in Xamarin form from View model从 Viewmodel 更改 Xamarin 表单中的背景颜色的问题
【发布时间】:2020-06-10 10:12:13
【问题描述】:

更改条目的文本值和条目的背景颜色有什么不同吗?

在此处使用代码...https://github.com/jamesmontemagno/app-compass。我可以添加一个条目

       <Entry Placeholder="Degree to follow:" 
               Text="{Binding Degree}"  x:Name="Degree" 
               Keyboard="Numeric" 
               Grid.Row="0"  BackgroundColor="Green"/>

在 ViewModel 中定义一个属性

       public string _degree;
        public string Degree
        {
            get => _degree; 
            set => SetProperty(ref _degree, value); 
        }

我可以从视图模型中更改条目..."_degree = "3333";" 我可以得到它的价值..."HeadingDisplay = $"Heading: {Heading.ToString()}" + " Degree: " + _degree;"

那么,为什么我不能以相同的方式更改条目、标签或页面之一的背景颜色? 我在这里添加了绑定...

       <Label Grid.Row="2" 
            Text="{Binding HeadingDisplay}" 
           x:Name="LabelInfo"
              TextColor="{Binding BGColor, Mode=OneWay}" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />

然后尝试在 ViewModel 中更改它,我尝试了一些不同的东西...

        public Color BGColor { get; set; }

        //public Color BgColor
        //{
        //    get => _bgcolor;
        //    set => SetProperty(ref _bgcolor, value);
        //}
               //_bgcolor = "#00FF00;
                //_bgcolor = Xamarin.Forms.Color.Red;
                BGColor = Color.Red;

背景颜色永远不会改变。我需要转换器吗?页面是否需要重新显示,还是什么?

根据下面的cmets,我做了一些更改,我现在绑定标签背景,而不是文本背景

        <Label Grid.Row="2" 
            Text="{Binding HeadingDisplay}" 
           x:Name="LabelInfo" BackgroundColor="{Binding=BGCOLOR Mode=OneWay}" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />

在视图模型中,这是现在的代码

 public Color _bgcolor;
        public Color BGCOLOR
        {
            get => _bgcolor;
            set => SetProperty(ref _bgcolor, value);
        }

//set the color here
               _bgcolor = Color.Red;

仍然没有颜色变化。我不清楚关于两种颜色类型的评论。你能指点我链接,或者举个例子吗?我也尝试过发送 FromHex。

【问题讨论】:

  • Degree 调用 SetProperty,BGColor 没有。这是触发 PropertyChanged 事件所必需的
  • 颜色是哪一个命名空间(来自您的财产)? Xamarin 有两种颜色类型。您必须使用正确的方式才能与视图绑定。
  • 我编辑了原始帖子以显示更改。当我尝试在此处添加新行时,它发布了评论。

标签: xamarin.forms colors


【解决方案1】:

这里是如何修改 Control 的ColorText 的示例,您可以参考检查问题所在。

创建一个 ViewModel 类:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Color color { set; get; }

    public Color Color
    {
        set
        {
            if (color != value)
            {
                color = value;
                OnPropertyChanged("Color");
            }
        }
        get
        {
            return color;
        }
    }

    private string textValue { set; get; }

    public string TextValue
    {
        set
        {
            if (textValue != value)
            {
                textValue = value;
                OnPropertyChanged("TextValue");
            }
        }

        get { return textValue; }
    }

    public ViewModel()
    {
        color = Color.Red;
        textValue = "Default Text";
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml 代码:

<StackLayout>
    <!-- Place new controls here -->
    <Label Text="{Binding TextValue}" TextColor="{Binding Color}"
       HorizontalOptions="Center"/>
    <Entry Text="{Binding TextValue}" TextColor="{Binding Color}"/>
    <Button Text="Change" Clicked="Button_Clicked"/>
</StackLayout>

在 ContentPage 中可以修改如下值:

public partial class MainPage : ContentPage
{
    ViewModel viewModel;
    public MainPage()
    {
        InitializeComponent();
        viewModel = new ViewModel();
        BindingContext = viewModel;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        viewModel.TextValue = "New Value";
        viewModel.Color = Color.Green;
    }
}

效果:

【讨论】:

  • 如上所述,我下载并尝试修改 .github.com/jamesmontemagno/app-compass 中的代码。那个View模型代码没有INotifyPropertyChanged,你是说我需要加吗?我不明白为什么我可以更改我添加的条目值,但不能更改背景颜色。
  • @Lou 是的,需要INotifyPropertyChanged。为什么背景颜色不起作用,可能是受其他代码影响。毕竟是为了实现其他一些功能而创建的项目。
猜你喜欢
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多