WPF -MVVM大型模型或大视图模型

发布于 2025-02-05 15:18:44 字数 2084 浏览 3 评论 0原文

我花了一些时间在WPF中搜索MVVM。我熟悉C#和WPF。我也了解MVVM及其好处。我真的不知道该如何进行视图和模型,因为我看到的多个示例并不总是一样。我知道MVVM是灵活的,您可以将其“适应”您的需求。

我将解释我的申请是关于什么,然后问我的问题。

我将简化交付和物品。两者都是SQL的表。我可以提出自己的SQL请求,并将行放入列表或观察力汇总的情况下,而没有问题。在应用程序中,您有一个交货列表,当您选择一个列表时,其所有项目都会显示在其旁边的另一个列表中。您可以添加/删除交货,添加/删除项目。您可以修改项目的数量。请注意,所有操作都会立即修改数据库。每个动作都有很多验证。

我已经以不太良好的方式构建了该应用程序(一个MainWindow和Code-Behind中的所有内容),即使很难,我也想使用MVVM对其进行重建,因为这是正确的方法。

因此,我要做的是有2种型号和2个视图型号(交付和项目)。我的旧应用程序有2个观测值,因此我想重新使用它来帮助我转移。我将InotifyPropertychanged放在视图模型中,或者对此具有基本视图模型。

问题:

我应该在模型中有一个观察力固定,并且在视图模型中有一个?在视图模型中只有一个?简单的列表会更好吗?

这有点取决于第一个问题:我是否应该在视图模型中收集数据库的数据并将其放入观察力?在模型中这样做会更好吗?我已经看到它应该在模型中,甚至应该在另一层中。

这也取决于第一个问题:我应该在哪里放置方法,查看模型或模型?假设AddDelivery()。我所做的是将交付量添加到我的ObservableCollection中,并同时更新数据库。因此,现在,如果我的视图模型中只有一个观察力,我需要在同一位置使用我的方法?如果您告诉我进行2个观测值怎么办。

在帮助我获得的帮助下

,我设法启动了我的新应用程序!

我现在陷入了一个部分,一个视图模型如何与另一部分进行通信?在这两个模型中,我都对propertychange进行了。选择交付时,我需要更改项目的观察力。当我选择时,我已经有了NotifyPropertychanged,我只需要从我的ItemViewModel中订阅该事件或向其发送消息?

模型:

    public class Delivery
    {
        public string name { get; set; }
        public string noDelivery{ get; set; }
    }


    public class Item
    {
        public string name { get; set; }
        public string noItem { get; set; }
    }

查看模型:

    public abstract class ViewModelBase : INotifyPropertyChanged 
    {
  

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
        {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }  
    }
    
    public class DeliveryViewModel : ViewModelBase
    {
        public ObservableCollection<Delivery> Deliveries{ get; private set;}
        //Some Functions
    }


    public class ItemViewModel
    {
        public ObservableCollection<Item> Items { get; private set; }

        //Some Functions
    }

我称之为“ threseviewiewModel”中的notifypropertychange,但我不知道其他视图模型如何对其作用。换句话说,当事件发生在交付视图模型中时,我想从itemViewModel调用函数。

I have spent some time searching about MVVM in WPF. I'm familiar with C# and WPF. I also understand MVVM and its benefits. I don't really know how to go about my view models and models as the multiple examples I've seen are not always the same. I know MVVM is flexible and that you can "adapt" it to your needs.

I'll explain what my application is about and then ask my questions.

I'll simplify to Deliveries and Items. Both are tables from SQL. I can make my own SQL requests and put the rows in a list or an observablecollection without a problem. In the application, you have a list of deliveries and when you select one, all its items appear on another list next to it. You can Add/Delete a delivery, Add/Delete an Item. You can modify the quantity of the Item. Note that all the actions modify the Database instantly. There is a lot of verification for each actions.

I've already built the application in a not-so-good way (one MainWindow and all the rest in the code-behind) and I'd like to rebuild it using MVVM even if it's hard because it would be the right way.

So what I'd do is have 2 models and 2 view models (Deliveries and Items). My old application had 2 observablecollections so I'd like to re-use that to help me transfer. I'd put INotifyPropertyChanged in the view models or maybe have a base view model for that.

Questions:

Should I have an observablecollections in the model and one in the view model? Only one in the view model? Simple lists would be better?

This kinda depends on the first question: should I gather the data from my DB in my ViewModel and put it in an observablecollection? Is it better to do that in the model? I've seen that it should be in the model or even another layer.

This kinda depends on the first question too: where should I put my methods, view model or model? Let's say AddDelivery(). What I did was add the delivery to my observablecollection and update the DB at the same time. So now if I have only one observablecollection in my View Model, I'd need to have my methods at the same place? What if you tell me to make 2 observablecollections.

Edit

With the help I got I managed to start my new app!

I'm stuck at one part now, how can one view model communicate with another one? I have INotifyPropertyChanged in both of those models. I need to change my observableCollection of Item when I select a delivery. I already have the NotifyPropertyChanged when I select I only need to Subscribe to the event from my ItemViewModel or send a message to it?

Models:

    public class Delivery
    {
        public string name { get; set; }
        public string noDelivery{ get; set; }
    }


    public class Item
    {
        public string name { get; set; }
        public string noItem { get; set; }
    }

View Models:

    public abstract class ViewModelBase : INotifyPropertyChanged 
    {
  

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
        {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }  
    }
    
    public class DeliveryViewModel : ViewModelBase
    {
        public ObservableCollection<Delivery> Deliveries{ get; private set;}
        //Some Functions
    }


    public class ItemViewModel
    {
        public ObservableCollection<Item> Items { get; private set; }

        //Some Functions
    }

I call NotifyPropertyChanged in my DeliveryViewModel but I don't know how can the other view model do something with it. In other words I want to call a function from ItemViewModel when an event happens in DeliveryViewModel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2025-02-12 15:18:44

答案:

  1. 您应该仅根据最佳实践而在视图模型中具有观察力。因为您的ViewModel将连接到视图而不是模型。

  2. 您应始终首先从模型中收集DB的数据。这就是关注点的分离方式。然后,您可以在ViewModel的观测值中添加相同的内容。

  3. 您的方法将直接与您的DB相互作用。添加交货详细信息后的最终列表应在ViewModel ObservableCollection中重新提交或更新,并分配给其新ID。

始终遵循关注的分离原则。因为MVVM全部大于松散耦合

谢谢。

Answers:

  1. You should have an observablecollections in the view model only as per the best practices. Because your viewModel is going to connect to View and not the model.

  2. You should always gather the data from DB in Model first. That is how the separation of concerns work right. Then you can add the same in ObservableCollection of Your ViewModel.

  3. You methods which will directly interact with your DB should be placed in Models. The final list after adding the delivery details shall be re-fetched or updated in ViewModel ObservableCollection with new Id assigned to it.

Always Follow Separation Of Concerns Principle. Because MVVM is all about loosely Coupling.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文