C# 从类构造函数实例化对象集合

C# 从类构造函数实例化对象集合,c#,collections,C#,Collections,我试图实例化一个类构造函数中的对象集合,但是出现了这样一个错误:成员名称不能与其封闭类型相同 namespace MVVM { public class MoviesPageViewModel { public class MoviesPageViewModel { public List<MovieCategory> Items { get; set; } public Movies

我试图实例化一个类构造函数中的对象集合,但是出现了这样一个错误:成员名称不能与其封闭类型相同

 namespace MVVM
{
    public class MoviesPageViewModel
    {
        public class MoviesPageViewModel
        {
            public List<MovieCategory> Items { get; set; }

            public MoviesPageViewModel()
            {
                var movies = new List<Movie>
                             {
                                 new Movie {Title = "The Ewok Adventure", Category = "Adventure", Subtitle = "The Towani family civilian shuttlecraft crashes on the forest moon of Endor.", Image = "http://cf2.imgobject.com/t/p/w500/y6HdTlqgcZ6EdsKR1uP03WgBe0C.jpg"},
                                 new Movie {Title = "The In-Laws", Category = "Adventure", Subtitle = "In preparation for his daughter's wedding, dentist Sheldon ", Image = "http://cf2.imgobject.com/t/p/w500/9FlFW9zhuoOpS8frAFR9cCnJ6Sg.jpg"},
                                 new Movie {Title = "The Man Called Flintstone", Category = "Adventure", Subtitle = "In this feature-length film based on the Flintstones TV show", Image = "http://cf2.imgobject.com/t/p/w500/6qyVUkbDBuBOUVVplIDGaQf6jZL.jpg"},

                                 new Movie {Title = "Super Fuzz", Category = "Comedy", Subtitle = "Dave Speed is no ordinary Miami cop--he is an irradiated Miami cop ", Image = "http://cf2.imgobject.com/t/p/w500/bueVXkpCDPX0TlsWd3Uk7QKO3kD.jpg"},
                                 new Movie {Title = "The Knock Out Cop", Category = "Comedy", Subtitle = "This cop doesn't carry a gun - his fist is loaded!", Image = "http://cf2.imgobject.com/t/p/w500/mzlw8rHGUSDobS1MJgz8jXXPM06.jpg"},
                                 new Movie {Title = "Best Worst Movie", Category = "Comedy", Subtitle = "A look at the making of the film Troll 2 (1990) ", Image = "http://cf2.imgobject.com/t/p/w500/5LjbAjkPBUOD9N2QFPSuTyhomx4.jpg"},

                                 new Movie {Title = "The Last Unicorn", Category = "Fantasy", Subtitle = "A brave unicorn and a magician fight an evil king", Image = "http://cf2.imgobject.com/t/p/w500/iO6P5vV1TMwSuisZDtNBDNpOxwR.jpg"},
                                 new Movie {Title = "Blithe Spirit", Category = "Fantasy", Subtitle = "An English mystery novelist invites a medium", Image = "http://cf2.imgobject.com/t/p/w500/gwu4c10lpgHUrMqr9CBNq2FYTpN.jpg"},
                                 new Movie {Title = "Here Comes Mr. Jordan", Category = "Fantasy", Subtitle = "Boxer Joe Pendleton, flying to his next fight", Image = "http://cf2.imgobject.com/t/p/w500/9cnWl7inQVX6wznjYNmQmJXVD6J.jpg"},
                             };

                var moviesByCategories = movies.GroupBy(x => x.Category).Select(x => new MovieCategory { Title = x.Key, Items = x.ToList() });

                Items = moviesByCategories.ToList();
            }
        }

        public class Movie
        {
            public string Title { get; set; }
            public string Subtitle { get; set; }
            public string Image { get; set; }
            public string Category { get; set; }
        }

        public class MovieCategory
        {
            public string Title { get; set; }
            public List<Movie> Items { get; set; }
        }
    }
}
好心协助

public class MoviesPageViewModel
{
    public class MoviesPageViewModel
它不是构造函数,它是一个内部类型-这是不允许的

尝试:


您有一个名为MoviesPageViewModel的类在MoviesPageViewModel中声明为类,这可能不是您想要的。

您的嵌套类与在中声明的类具有相同的名称。这是不可能的:

  public class MoviesPageViewModel
    {
        public class MoviesPageViewModel
        {
只需删除第一个或将其更改为类似的内容:

  public class MoviesPageViewModel
    {
        public class OtherClassName
        {

出于某种原因,您有两个MoviesPageViewModel类typo?的声明?。删除一个:

namespace MVVM
{
    public class MoviesPageViewModel
    {
        public List<MovieCategory> Items { get; set; }

        public MoviesPageViewModel()
        {
你有两部电影PageViewModel一部在另一部里面。在.NET中是不允许的
namespace MVVM
{
    public class MoviesPageViewModel
    {
        public List<MovieCategory> Items { get; set; }

        public MoviesPageViewModel()
        {