我对xaml和xamarin.forms很新,我在使用Data Binding时遇到了一些麻烦。我知道有很多类似的问题,但我发现似乎没有任何效果。我的实际目标是在打开和关闭开关时更改背景颜色。但是现在我只想进行绑定工作,所以我在示例中使用了一个字符串。我的Page.xaml.cs看起来像这样
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CounterMainPage : ContentPage
{
public CounterMainPage ()
{
InitializeComponent ();
BindingContext = new CounterMainPageViewModel();
}
我的viewmodel看起来像这样:
class CounterMainPageViewModel
{
public Color BackGroundColor = Color.Red;
public string Testning = "Urgent";
}
My Converter类看起来像这样:
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var valueAsString = value.ToString();
switch (valueAsString)
{
case ("Urgent"):
{
return Color.FromHex("#EF3D60");
}
case ("Not urgent"):
{
return Color.FromHex("#231F20");
}
default:
{
return Color.FromHex("#231F20");
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最后但并非最不重要的是我的page.xaml看起来像这样:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:c="clr-namespace:BlankClient"
xmlns:local="clr-namespace:BlankClient"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BlankClient.View.CounterMainPage">
<!--Style="{StaticResource PageStyle}">-->
<ContentPage.Resources>
<ResourceDictionary>
<c:BoolToColorConverter x:Key="colorConverter"></c:BoolToColorConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label x:Name="ReceivedFromServer" Text="" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>
<Button Text="A" StyleId="0" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="0"/>
<Button Text="2" StyleId="1" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="1" />
<Button Text="3" StyleId="2" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="2"/>
<Button Text="4" StyleId="3" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="0"/>
<Button Text="5" StyleId="4" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="1"/>
<Button Text="6" StyleId="5" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="2"/>
<Button Text="7" StyleId="6" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="0"/>
<Button Text="8" StyleId="7" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="1"/>
<Button Text="9" StyleId="8" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="2"/>
<Button Text="10" StyleId="9" Clicked="Button_Clicked" Grid.Row="4" Grid.Column="0" BackgroundColor="{Binding Testning,Converter={StaticResource colorConverter}}"/>
<Button Text="Undo" StyleId="Undo" Clicked="Button_Clicked" Grid.Row="4" Grid.Column="1" IsEnabled="False" />
<Button Text="Redo" StyleId="Undo " Clicked="Button_Clicked" Grid.Row="4" Grid.Column="2" IsEnabled="False"/>
<Switch Grid.Row="5" Grid.Column="0" />
</Grid>
<!--<StackLayout>
<Label Text="Counter Main Page" />
<Button Text="5" StyleId="5" Clicked="Button_Clicked"/>
<Button Text="10" StyleId="10" />
<Label x:Name="ReceivedFromServer" Text="ewq"/>
</StackLayout>-->
</ContentPage.Content>
答案 0 :(得分:0)
首先“公共字符串测试=”紧急“;”应该是将其绑定到视图的属性。读取输出,您将看到存在未找到“Testning”属性的异常。
第二:你需要实现INotifyPropertyChanged接口(或使用类似Prism的框架)才能更改属性值并通知UI有关该更改