首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xamarin.Forms中使用转换器替换图像

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

Stack Overflow用户
提问于 2017-05-02 17:10:35
回答 3查看 4.2K关注 0票数 1

我试图使用Image中的Converters来动态地更改基于数据下载的Xamarin.Forms源代码。

从服务器获取数据的总共有三种状态

1)成功下载数据时成功;2)未下载数据时出错;3)进程空闲时出错

对于所有上述情况,我使用的是不同的图标。

这是我的XAMLcode

代码语言:javascript
复制
 <Image Source="{Binding CustomerState,  Converter={StaticResource SyncConverter}}"  HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="0" HeightRequest="20" Margin="8,12,8,12" />

这是我的转换器代码

代码语言:javascript
复制
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为真,则显示成功,否则出现错误。

我的视图模型代码

代码语言:javascript
复制
private bool? isCustomerState;

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


            }
        }

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

System.NullReferenceException:对象引用没有设置为对象的实例。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-02 17:13:10

在使用“值”之前,您可以尝试验证“值”。有点像

代码语言:javascript
复制
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();
        }
    }
票数 7
EN

Stack Overflow用户

发布于 2017-05-02 18:33:17

更新: Alessandro在注释中提到的--转换器提供的实例的返回类型在本例中可能不是实际问题。https://github.com/xamarin/Xamarin.Forms/blob/e6d5186c8acbf37b877c7ca3c77a378352a3743d/Xamarin.Forms.Core/ImageSource.cs应该处理从字符串到ImageSource的转换。

由于您正在绑定到图像上的ImageSource类型的Source属性,所以您的转换器还应该返回ImageSource类型的实例。

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2017-05-03 07:22:14

代码语言:javascript
复制
  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;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43743629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档