[英]Binding BackgroundColor properties ContentPage in Xamarin Forms
I've need to binding background color from string. 我需要绑定字符串的背景颜色。 My xaml code: 我的xaml代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Fimap.LoadingPage"
BackgroundColor="{Binding ColorBackground}">
<ContentPage.Content>
<Grid Padding="130" x:Name="griglia">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10*"></RowDefinition>
<RowDefinition Height="40*"></RowDefinition>
<RowDefinition Height="25*"></RowDefinition>
<RowDefinition Height="25*"></RowDefinition>
</Grid.RowDefinitions>
<Image Source="logo.png" Grid.Row="1"></Image>
<ActivityIndicator x:Name="loading" Grid.Row="2" IsVisible="true" Color="{Binding ColorBackground}" IsRunning="true" />
</Grid>
</ContentPage.Content>
</ContentPage>
my codebehind code: 我的代码隐藏代码:
...
public String ColorBackground { get; set; } = "#E40000";
...
I did set this ColorBackground
before the public Class() costructor. 我确实在公共的Class()构造ColorBackground
之前设置了这个ColorBackground。
But don't work...where do I wrong ? 但是不要工作......我哪里错了?
Thanks to all 谢谢大家
You either need to bind to a Xamarin.Forms.Color
, like this: public Color ColorBackground { get; set; } = Color.FromHex("#E40000");
您需要绑定到Xamarin.Forms.Color
,如下所示: public Color ColorBackground { get; set; } = Color.FromHex("#E40000");
public Color ColorBackground { get; set; } = Color.FromHex("#E40000");
Of you need a IValueConverter
to convert the string to a color. 你需要一个IValueConverter
来将字符串转换为颜色。
To make data binding work, make sure that you set the BindingContext
property of your page, like this: BindingContext = this;
要使数据绑定起作用,请确保设置页面的BindingContext
属性,如下所示: BindingContext = this;
Use this
if you use properties which are also in the page code-behind. 如果您使用也在页面代码隐藏中的属性,请使用this
。 If you want to use any other class as your view model you can set that as a BindingContext as well. 如果要将任何其他类用作视图模型,也可以将其设置为BindingContext。
You might want to look into a MVVM framework like FreshMvvm of MvvmCross to make your life a bit easier. 您可能希望研究像MvvmCross的FreshMvvm这样的MVVM框架,让您的生活更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.