使用Xamarin.Forms中的转换器替换图像

时间:2017-05-02 17:10:36

标签: c# xamarin xamarin.forms

我正在尝试使用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为空,则在上面的代码中

,然后显示ic_idle图标,如果CuswtomerStat为true,则显示成功,否则出错。

我的视图模型代码

private bool? isCustomerState;

public bool? CustomerState
        {
            get { return isCustomerState; }
            set
            {
                isCustomerState = value;
                OnPropertyChanged("CustomerState");


            }
        }

但不知何故,xamarin在get { return isCustomerState; }抛出错误,错误是

  

System.NullReferenceException:未将对象引用设置为实例   一个对象。

3 个答案:

答案 0 :(得分:4)

您可以尝试验证&#34;价值&#34;在使用之前。像

这样的东西
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();
        }
    }

答案 1 :(得分:0)

更新: 正如Alessandro在评论中所提到的 - 转换器提供的实例的返回类型可能不是这种情况下的实际问题。 ImageSourceConverter应该处理从字符串到ImageSource的转换。

当你绑定Image类型为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}"));
    }
}

您可以在此处找到更多详细信息: https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Image_and_ImageSource

答案 2 :(得分:0)

  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;
        }
    }