ObservableCollection <t>(List <t>)构造函数不起作用

时间:2015-12-13 13:43:25

标签: c# .net windows-10 uwp

我不确定我是否遗漏了一些非常基本的东西,或者这个构造函数是否存在问题。

public class SingleQuestion : INPC
{
    private String _questionText;
    private EDifficulty _difficulty;
    private ObservableCollection<String> _answerList;
    private ObservableCollection<Image> _imageList;

    public String QuestionText
    {
        get
        {
            return _questionText;
        }
        set
        {
            if (String.IsNullOrEmpty(value) || value == _questionText)
                return;

            _questionText = value;
            OnPropertyChanged();
        }
    }

    public EDifficulty Difficulty
    {
        get
        {
            return _difficulty;
        }
        set
        {
            if (_difficulty == value)
                return;

            _difficulty = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<String> AnswerList
    {
        get
        {
            return _answerList;
        }
        set
        {
            if (value == null)
                return;

            _answerList = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<Image> ImageList
    {
        get
        {
            return _imageList;
        }
        set
        {
            if (_imageList == null)
                return;

            _imageList = value;
            OnPropertyChanged();
        }
    }

    public SingleQuestion(String questionText, EDifficulty difficulty, List<String> answers, List<Image> images)
    {
        QuestionText = questionText;
        Difficulty = difficulty;
        AnswerList = new ObservableCollection<string>(answers);
        ImageList = new ObservableCollection<Image>(images);
    }
}

ImageList始终为空,我不知道为什么。 images至少是一个空列表或其中包含项目,因此ImageList至少应使用空列表进行初始化。

Image上课:

public class Image : INPC
{
    private String _imagePath;
    private bool _overwrite;

    public String ImagePath
    {
        get { return _imagePath; }
        set
        {
            _imagePath = value;
            OnPropertyChanged();
        }
    }

    public bool Overwrite
    {
        get { return _overwrite; }
        set
        {
            _overwrite = value;
            OnPropertyChanged();
        }
    }

    public Image(String imgPath, bool overwrite)
    {
        ImagePath = imgPath;
        Overwrite = overwrite;
    }
}

我没有找到任何关于这个问题的引用,有人可以给我任何提示吗?

更新:

我添加了一个测试用例。正如您所看到的那样,构造函数在test变量处按预期工作,但不在SingleQuestion类的构造函数中。我还包括完整的SingleQuestion课程。

enter image description here

1 个答案:

答案 0 :(得分:3)

是不是因为有人?

初始化时,

_imageList始终为null。所以,setter只返回。

 public ObservableCollection<Image> ImageList
    {
        get
        {
            return _imageList;
        }
        set
        {
////THIS CHECK
            if (_imageList == null)
                return;


            _imageList = value;
            OnPropertyChanged();
        }
    }

你可以直接在setter中设置它:

  set
   {
    _imageList = value
    OnPropertyChanged();
    }