【发布时间】: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