xamarin 如何在C#中将标签颜色设置为动态资源的颜色?

ar7v8xwq  于 2022-12-07  发布在  C#
关注(0)|答案(2)|浏览(133)

下面是我的代码:

grid.Children.Add(new Label
{
   Text = "00",
   VerticalOptions = LayoutOptions.Center,
   HorizontalOptions = LayoutOptions.Center,
   TextColor = " {DynamicResource PageBackgroundColor}"
});

但它不接受动态资源设置的格式。

vltsax25

vltsax251#

发生这种情况是因为您使用了错误的语法,请尝试以下操作:

TextColor = (Color) App.Current.Resources["PageBackgroundColor"];

如果动态资源在运行时不断变化,请执行以下操作:

yourLabel.SetDynamicResource (VisualElement.TextColorProperty, "PageBackgroundColor");

如有疑问,请回复

dy2hfwbg

dy2hfwbg2#

yourLabel.SetDynamicResource (Label.TextColorProperty, "PageBackgroundColor");

相关问题