我试图使用Image中的Converters来动态地更改基于数据下载的Xamarin.Forms源代码。
从服务器获取数据的总共有三种状态
1)成功下载数据时成功;2)未下载数据时出错;3)进程空闲时出错
对于所有上述情况,我使用的是不同的图标。
这是我的XAMLcode
<Image Source="{Binding CustomerState, Converter={StaticResource SyncConverter}}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="0" HeightRequest="20" Margin="8,12,8,12" />这是我的转换器代码
public class SyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool? syncState = value as bool?;
if (syncState != null) {
if (syncState.Value) return "ic_success";
else return "ic_error";
}
return "ic_idle";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}在上面的代码中,如果CustomeState为null,则显示ic_idle图标,如果CuswtomerStat为真,则显示成功,否则出现错误。
我的视图模型代码
private bool? isCustomerState;
public bool? CustomerState
{
get { return isCustomerState; }
set
{
isCustomerState = value;
OnPropertyChanged("CustomerState");
}
}但不知何故,xamarin向get { return isCustomerState; }抛出了错误,错误是
System.NullReferenceException:对象引用没有设置为对象的实例。
发布于 2017-05-02 17:13:10
在使用“值”之前,您可以尝试验证“值”。有点像
public class SyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value != null){
bool? syncState = value as bool?;
if (syncState != null) {
if (syncState.Value) return "ic_success";
else return "ic_error";
}
}
return "ic_idle";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}发布于 2017-05-02 18:33:17
更新: Alessandro在注释中提到的--转换器提供的实例的返回类型在本例中可能不是实际问题。https://github.com/xamarin/Xamarin.Forms/blob/e6d5186c8acbf37b877c7ca3c77a378352a3743d/Xamarin.Forms.Core/ImageSource.cs应该处理从字符串到ImageSource的转换。
由于您正在绑定到图像上的ImageSource类型的Source属性,所以您的转换器还应该返回ImageSource类型的实例。
public class SyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value != null){
bool? syncState = value as bool?;
if (syncState != null) {
if (syncState.Value) return ConvertToImageSource("ic_success");
else return ConvertToImageSource("ic_error");
}
}
return ConvertToImageSource("ic_idle");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private ImageSource ConvertToImageSource(string fileName)
{
return Device.OnPlatform(
iOS: ImageSource.FromFile($"Images/{fileName}"),
Android: ImageSource.FromFile(fileName),
WinPhone: ImageSource.FromFile($"Images/{fileName}"));
}
}您可以在这里找到更多详细信息:ImageSource
发布于 2017-05-03 07:22:14
public class SyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
return ConvertToImageSource("ic_idle");
return (bool)value? ConvertToImageSource("ic_success"): ConvertToImageSource("ic_error");
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return (bool)value;
}
}https://stackoverflow.com/questions/43743629
复制相似问题