Mr.*_*per 2 c# binding xamarin.forms
我需要绑定字符串的背景颜色.我的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>
Run Code Online (Sandbox Code Playgroud)
我的代码隐藏代码:
...
public String ColorBackground { get; set; } = "#E40000";
...
Run Code Online (Sandbox Code Playgroud)
我确实ColorBackground在公共Class()costructor之前设置了它.
但是不要工作......我哪里错了?
谢谢大家
Ger*_*uis 11
您需要绑定到a Xamarin.Forms.Color,如下所示:public Color ColorBackground { get; set; } = Color.FromHex("#E40000");
你需要IValueConverter将字符串转换为颜色.
要使数据绑定起作用,请确保设置BindingContext页面的属性,如下所示:BindingContext = this;
使用this,如果你使用的是同样在页面代码隐藏属性.如果要将任何其他类用作视图模型,也可以将其设置为BindingContext.
您可能希望研究像MvvmCross的FreshMvvm这样的MVVM框架,让您的生活更轻松.
| 归档时间: |
|
| 查看次数: |
2894 次 |
| 最近记录: |