【问题标题】:Struggling with nested lists in c#在 C# 中与嵌套列表作斗争
【发布时间】:2020-03-24 15:49:13
【问题描述】:

我在很多未知领域工作,因为我试图在这个项目中正确遵循 MVVM 以及正确的结构。我根据未来 json 文档的布局方式创建了以下类:

    public class SkinList
    {
        public class Coord
        {
            public string x { get; set; }
            public string y { get; set; }
        }

        public class Location
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public List<Coord> Coords { get; set; }
            public string CodeNumber { get; set; }
            public string Description { get; set; }
        }

        public class Skin
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public List<Location> Locations { get; set; }
        }

        public class RootObject
        {
            public List<Skin> Skins { get; set; }
        }
    }

当前页面在加载时会创建一个新的Skin(皮肤)对象。它允许用户单击图像上的一个点。生成该点时,我希望它创建一个新的 Location 对象(点)。我希望屏幕上有多个不同的框来绑定到该对象的各种属性。我的下一步将是弄清楚整个 INotify 的事情,但我被困在一个部分。我似乎无法弄清楚如何将 Location 对象附加到 Skin 对象中的 Locations 列表中。我知道我只是缺少一些逻辑,这可能是非常直接的。

这是我正在使用的代码隐藏:

{
    /// <summary>
    /// Interaction logic for ManikinEditor.xaml
    /// </summary>
    public partial class ManikinEditor : Page
    {
        public List<SkinList.Skin> skin = new List<SkinList.Skin>();

        private Ellipse elip = new Ellipse();
        private Point anchorPoint;
        private int Radius = 3;
        public ManikinEditor()
        {
            string guid = System.Guid.NewGuid().ToString();
            SkinList.Skin _skin = new SkinList.Skin();
            _skin.ID = guid;
            skin.Add(_skin);
            InitializeComponent();


        }
        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            anchorPoint = e.MouseDevice.GetPosition(Cnv);
            //public List<SkinList.Skin
            //           SkinList.Location point = new SkinList.Location();
 //           List<SkinList.List> location = new List<SkinList.Locations>();
            SkinList.Location point = new SkinList.Location();
            string pointguid = System.Guid.NewGuid().ToString();
            point.ID = pointguid;
            Path test = new Path
            {
                Stroke = Brushes.Red,
                StrokeThickness = 1,
                Fill = Brushes.Black,
                Data = new EllipseGeometry { Center = anchorPoint, RadiusX = Radius, RadiusY = Radius }
            };

            Cnv.Children.Add(test);


        }

    }
}

在我把所有东西放在一起之后,我会将整个东西序列化并添加到 json 文件中的列表(皮肤)中,但是当我遇到它时,我会跨越这个障碍。

【问题讨论】:

  • 您的问题是什么?你能运行你的代码吗?如果没有,你会得到什么错误?如果是这样,会发生什么?您希望它做些什么不同的事情?
  • 嗨代码。我的问题是关于如何让它按照描述的方式工作。无法编译,因为我无法弄清楚我应该做什么才能使其工作。我对这些领域中的许多领域都很陌生,不幸的是,COVID 都剥夺了我将要参加的 CE 课程,只给了我时间从事这个项目。

标签: c# list class


【解决方案1】:

有点回答我自己的问题。这只是我将列表对象视为类对象的一种情况(不确定我的措辞是否正确,我很新)。以下是目前看来有效的更新代码:

    public partial class ManikinEditor : Page
    {
        public SkinList.Skin skin = new SkinList.Skin();
        List<SkinList.Location> location = new List<SkinList.Location>();
        private Ellipse elip = new Ellipse();
        private Point anchorPoint;
        private int Radius = 3;
        public ManikinEditor()
        {
            string guid = System.Guid.NewGuid().ToString();
            skin.ID = guid;
            InitializeComponent();


        }
        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            anchorPoint = e.MouseDevice.GetPosition(Cnv);
            //public List<SkinList.Skin
            //           SkinList.Location point = new SkinList.Location();

            SkinList.Location point = new SkinList.Location();
            SkinList.Coord coord = new SkinList.Coord();
            coord.x = (int)anchorPoint.X;
            coord.y = (int)anchorPoint.Y;
            string pointguid = System.Guid.NewGuid().ToString();       
            point.ID = pointguid;
            point.Coords = coord;
            location.Add(point);
            //           skin.Add(location);
            skin.Locations = location;
            Path test = new Path
            {
                Stroke = Brushes.Red,
                StrokeThickness = 1,
                Fill = Brushes.Black,
                Data = new EllipseGeometry { Center = anchorPoint, RadiusX = Radius, RadiusY = Radius }
            };

            Cnv.Children.Add(test);


        }

    }

我想另一个后续行动是,如果这段代码适用于遵循 MVVM 模型的代码隐藏?鉴于代码有限,我不知道这个问题是否真的可以回答,但我这次努力做得很好,做对了。

【讨论】:

    【解决方案2】:

    好的。 首次尝试 MVVM 时可能会有点令人生畏,所以这里有一些提示。

    您创建的类实际上是数据模型。

    您应该尝试并且仅将支持视图所需的代码放在其后面的代码中。

    诸如创建模型类实例和设置它们的程序逻辑应该在 ViewModel 中执行,它顺便实现了 INotifyPropertyChanged 的​​东西。

    顺便说一句,对于单独的 Model 和 ViewModel 类的需求通常存在灰色地带。

    您的视图应该具有绑定到 ViewModel 属性的数据和 ICommand 实例以执行操作,例如触发某些代码运行,例如json序列化。

    你的 SkinList ViewModel 应该有一个皮肤的 ObservableCollection。

    就个人而言,我会将我的类分开,这样它们就不会相互嵌入,将每个类放在自己的文件中。

    每个类都应该能够创建、删除和拥有它在列表中保存的类的选定实例 - 这将简化 XAML 中的绑定。 例如

    不要害怕实现 ViewModel 的 ObservableCollections 或将 ViewModel 放置在 ViewModel 中 - 问问自己这个问题,您想单独查看位置、皮肤还是坐标?

    如果您希望在一个用户视图中显示多个皮肤和位置,是否可以通过在视图中具有多个视图或在视图中具有多个自定义用户控件来最好地实现这一点?

    例如,您是否希望拥有一个显示多个皮肤层或仅显示一个皮肤的视图?

    编辑

    将鼠标单击坐标传递回 ViewModel 的最简单方法是使用后面的代码来捕获它并将其传递(您可以通过在 XAML 中进行绑定来实现,它有点复杂)。

    ViewModel 是视图的(页面)BindingContext(这可以在 XAML 或后面的代码中设置)。在它背后的代码中,它通常是通过构造函数传入的。

    然后您将使用您的“Image_MouseLeftButtonDown_1”处理程序引用它,并调用一个将坐标作为命令参数传递的命令。

    使用 ICommands 和绑定有助于将 UI 和程序代码解耦,同时允许它们在不同的线程上运行,保持 UI 响应。

    【讨论】:

    • 嗨,克里斯。非常感谢你的回复。我以我的方式创建课程的原因是基于这个 json 指南:c-sharpcorner.com/article/from-zero-to-hero-in-json-with-c-shar 如果我没看错,你的意思是这是我的模型,我需要创建一个视图模型在 this 和 view 之间进行交互?我会读一些你在这里谈论的内容。我已经准备好继续学习以正确地学习这一点,然后 COVID 夺走了这个机会,并给了我大量的时间来完成这个项目。
    • 为了回答您的其他问题,这实际上将在 JSON 文件中为每个“皮肤”创建一个配置文件。然后可以稍后将这些加载到不同的页面中,以配置应用程序的工作方式。一次只能查看一个。保存后,页面将离开或出现新皮肤。至于位置,每次在画布区域单击鼠标都会生成一个新位置。
    • 当鼠标被点击时,我如何从视图中获取 x 和 y 坐标到视图模型?
    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多