我需要从字符串中绑定背景颜色。我的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>
我的CodeBehind代码:
...
public String ColorBackground { get; set; } = "#E40000";
...
我确实将此ColorBackground
设置在公共类()coductor。
但是不工作...我在哪里错了?
感谢所有
您要么需要绑定到 Xamarin.Forms.Color
,因此: public Color ColorBackground { get; set; } = Color.FromHex("#E40000");
您需要IValueConverter
将字符串转换为颜色。
要使数据绑定起作用,请确保您设置页面的BindingContext
属性,例如:BindingContext = this;
如果使用页面代码范围中的属性,请使用this
。如果您想将任何其他类用作视图模型,则可以将其设置为bindingContext。
您可能想查看MVVM框架,例如MVVMCross的FreshMVVM,以使您的生活更轻松。