从 class 构造函数实例化对象集合

Instantiate collection of object from class constructor

我正在尝试从 class 构造函数实例化一个对象集合,但是我遇到了这个错误 "member names cannot be the same as their enclosing type"

 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

它不是构造函数,它是内部类型 - 不允许!

尝试:

public class MoviesPageViewModel
{
    public MoviesPageViewModel(){
    ...
    }
}

您有一个名为 MoviesPageViewModel 的 class 声明为 MoviesPageViewModel 内部的 class,这可能不是您想要的。

您的嵌套 class 与它在其中声明的 class 同名。这是不可能的:

  public class MoviesPageViewModel
    {
        public class MoviesPageViewModel
        {

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

  public class MoviesPageViewModel
    {
        public class OtherClassName
        {

出于某种原因,您有两个 MoviesPageViewModel 声明 class(打字错误?)。删除一个:

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

        public MoviesPageViewModel()
        {