我的抽象泛型类遇到了一些问题:
public abstract class ViewModel<TPrimaryModel> : ViewModel
where TPrimaryModel : TwoNames, new()
{
private ObservableCollection<ViewModel<TPrimaryModel>> _searchResults = new ObservableCollection<ViewModel<TPrimaryModel>>();
public ObservableCollection<ViewModel<TPrimaryModel>> SearchResults
{
get { return _searchResults; }
set
{
if (_searchResults != value)
{
_searchResults = value;
RaisePropertyChanged("SearchResults");
}
}
}
}
我的具体课程是:
public class RecipientViewModel : ViewModel<Recipient>
{
protected override void Find()
{
try
{
SelectedSearchResult = null;
//Following row leads to an error
SearchResults = new ObservableCollection<RecipientViewModel>();
...
}
}
}
此行会导致错误:
SearchResults = new ObservableCollection<RecipientViewModel>();
编译器消息是:
Cannot implicitly convert type
'System.Collections.ObjectModel.ObservableCollection<...RecipientViewModel>' in
'System.Collections.ObjectModel.ObservableCollection<...ViewModel<...Recipient>>'
收件人是一个模型,继承自TwoNames。