如何将List转换为隐式运算符类型

时间:2015-12-31 12:50:08

标签: c# .net wpf mvvm

我正在尝试学习MVVM,在我的ViewModel中我添加了隐式运算符,它将Model转换为ViewModel,反之亦然,但现在问题是如何将List of Model转换为ViewModel列表?

下面是我尝试用于列表转换但不起作用的结构代码:

Person.cs

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

PersonViewModel.cs

 class PersonViewModel : Person
{
    public string FullName { get; set; }

    public static implicit operator List<PersonViewModel>(IList<Person> person)
    {
        if (person == null)
            return null;

        return person.Select(c => new PersonViewModel(c)).ToList(); // This is not working

    }

    public static implicit operator PersonViewModel(Person person)
    {
        return new PersonViewModel
        {
            FirstName = person.FirstName,
            LastName = person.LastName,
        };
    }

    public static implicit operator Person(PersonViewModel personViewModel)
    {
        return new Person
        {
            FirstName = personViewModel.FirstName,
            LastName = personViewModel.LastName,
        };
    }

}

2 个答案:

答案 0 :(得分:0)

尝试使用Cast

person.Cast<PersonViewModel>().ToList();

已更新

这会给你其他错误。您可以使用扩展方法并在需要时显式调用。像这样的东西(确保你最终检查空值)

    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class PersonViewModel : Person
    {
        public string FullName { get; set; }
    }

    static class ConversionHelpers
    {
        public static Person ToPerson(this PersonViewModel pvm)
        {
            return new Person()
            {
                FirstName = pvm.FirstName,
                LastName = pvm.LastName
            };
        }

        public static PersonViewModel ToPersonViewModel(this Person p)
        {
            return new PersonViewModel()
            {
                FirstName = p.FirstName,
                LastName = p.LastName
            };
        }

        public static IEnumerable<PersonViewModel> ToPersonViewModels(IEnumerable<Person> persons)
        {
            return persons.Select(p => p.ToPersonViewModel());
        }
    }

答案 1 :(得分:0)

我很抱歉,但是你的MVVM模式&#34;似乎对我来说很奇怪 这是我对 MVVM模式

的理解
MyModuleHandler = logging.handlers.RotatingFileHandler('MyModule.log', maxBytes= 5000000, backupCount=5)
MyModuleHandler.setFormatter(formatter)
MyModuleHandler.setLevel(logging.DEBUG)


specialHandler = logging.handlers.RotatingFileHandler('special.log', maxBytes= 5000000, backupCount=5)
specialHandler.setFormatter(formatter)
specialHandler.setLevel(logging.INFO)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)

logging.getLogger('MyModule.special').setLevel(logging.DEBUG)
logging.getLogger('MyModule.special').addHandler(specialHandler)

logging.getLogger('MyModule').addHandler(console)
logging.getLogger('MyModule').setLevel(logging.DEBUG)
logging.getLogger('MyModule').addHandler(MyModuleHandler)

编辑作为@Mark Feldman评论的回答 如果包装似乎不必要或代码膨胀,那么&#34; ModelWrapper&#34;可以跳过,//Model class Person { public string FirstName { get; set; } public string LastName { get; set; } } //ModelWrapper which implements INotifyPropertyChanged if Model doesn't //Here you can add additional properties which will serve a View's needs class PersonModelWrapper { private Person _Model; //Through this constructor you will create instance of modelwrapper //without any conversions public PersonModelWrapper (Person model) { _Model = model; } public string FirstName { get { return _Model.FirstName; } set { _Model.FirstName = value; } } public string LastName { get { return _Model.LastName; } set { _Model.LastName = value; } } public string FullName { get { return _Model.FirstName + " " _Model.LastName; } } //Through this property you will always access current model property //without conversions public Person Model { get { return _Model; }} } //ViewModel which used as DataContext in the View class PersonViewModel { private PersonModelWrapper _Wrapper; public string Wrapper { get { return _Wrapper; } set { _Wrapper = value; } } } 可以由模型本身实现。 如果您不关心通知视图有关模型属性更改的信息,那么您不需要将模型集合转换为ViewModel集合。
您的ViewModel将包含Type ObservableCollection

的属性
INotifyPropertyChanged

没有包装的ViewModel

class PersonViewModel
{
    public ObservableCollection<Person> Persons { get; set; }

    //if you don't care about tracking changes of collection(Add, Remove)
    //then use only List<Person> without any conversions
   public List<Person> Persons { get; set; }

}

然后通过构造函数

以相同的方式将List of Models转换为ViewModel的集合