【发布时间】:2017-05-02 17:10:35
【问题描述】:
我正在尝试根据在Xamarin.Forms 中使用Converters 的数据下载动态更改Image 源
从服务器获取数据共有三种状态
1) 数据下载成功时成功 2) 数据未下载出现错误时报错 3) 进程空闲时
对于上述所有情况,我使用不同的图标。
这是我的 XAML 代码
<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 为空,则显示ic_idle 图标,如果CuswtomerStat 为真,则显示成功,否则显示错误。
我的视图模型代码
private bool? isCustomerState;
public bool? CustomerState
{
get { return isCustomerState; }
set
{
isCustomerState = value;
OnPropertyChanged("CustomerState");
}
}
但不知何故,xamarin 在get { return isCustomerState; } 处抛出错误,错误是
System.NullReferenceException:对象引用未设置为实例 一个对象。
【问题讨论】:
标签: c# xamarin xamarin.forms