我是xamarin表格和c#的新手。我正在使用WCF Web服务。我得到一个列表,并希望检查一个条件,并根据该条件,我想更新列表视图。 我搜索过文章,但没有创建我想要的东西。 到现在为止我已经做到了。
IEnumerable<LoadTrackedEmployee> _trackedEmployee;
private void Client_LoadTrackingEmployeeCompleted(object sender, LoadTrackingEmployeeCompletedEventArgs e)
{
var response = e.Result.ToString();
var result = JsonConvert.DeserializeObject<List<LoadTrackedEmployee>>(response);
Device.BeginInvokeOnMainThread(() =>
{
_trackedEmployee = result as IEnumerable<LoadTrackedEmployee>;
employeeList.ItemsSource = _trackedEmployee;
foreach(var item in _trackedEmployee)
{
if(item.int_LastLogin==null)
{
// I am trying to change it here. I have given label x:name="status" and i am trying to do this
status.TextColor = "Red"; // for some reason this doesnt work
}else{ status.TextColor = "Green"; }
}
});
}
IValueConverter示例
LoadTrackedEmployee.cs
public class LoadTrackedEmployee
{
public string vchar_UserGUID { get; set; }
[JsonProperty(PropertyName = "User ID")]
public string UserID { get; set; }
[JsonProperty(PropertyName = "Full Name")]
public string FullName { get; set; }
[JsonProperty(PropertyName = "Official Email ID")]
public string OfficialEmailID { get; set; }
public int? int_LastLogin { get; set; }
}
ListToColorConerter.cs
public class ListToColorConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
//What should i do here
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
XAML
<ContentPage.Resources>
<ResourceDictionary>
<local:StringToColorConverter x:Key="ListConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ListView x:Name="employeeList"
HasUnevenRows="True"
SeparatorVisibility="Default"
ItemSelected="employeeList_ItemSelected"
SeparatorColor="Black"
Margin="5,0,5,0">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label x:Name="status" BackgroundColor="{Binding ., Converter={StaticResource ListToColorConverter }}}" WidthRequest="3" HorizontalOptions="Start" Margin="0,2,0,2"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我还添加了本地xmlns
答案 0 :(得分:0)
文档是您的朋友:TextColor类的Label类型为Color,因此您可以选择已经找到的颜色之一:
status.TextColor = Color.Red;
或创建自己的:
status.TextColor = new Color(255, 255, 255);
如果您想为每个列表视图行分别使用颜色,则需要:
1)按如下方式定义列表视图,因此它包含名称和颜色的绑定:
<ListView x:Name="employeeList">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName}" TextColor="{Binding Color}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
2)为列表的每一行创建一个viewmodel类:
public class Employee
{
public string DisplayName {get;set;}
public Color Color {get;set;}
}
3)创建您的实际视图模型:
List<Employee> employees = new List<Employee> { new Employee { DisplayName = "John", Color= Color.Green } };
4)将您的列表附加为listview源:
employeeList.ItemsSource = employees;
我没有测试代码,但你应该弄明白。
答案 1 :(得分:0)
在你的情况下,我会在LoadTrackedEmployee
中引入额外的属性,它将根据你的条件返回一个布尔值。在绑定到此属性时使用IValueConverter
,如果为true则返回颜色X,如果为false则返回红色。
文档:
Binding Value Converters
From Data Bindings to MVVM
修改强>
从现在开始,您稍微改变了一个问题,就是IValueConverter
中应该有的内容:
public class ListToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value is LoadTrackedEmployee)
return (value as LoadTrackedEmployee).int_LastLogin.HasValue() ? Color.Red : Color.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
或者,您可以绑定到int_LastLogin
而不是整个模型。
P.S。:请遵循C#命名惯例。