WPF中的粘滞便笺项目。模型,视图,ViewModel

[英]Sticky Notes project in WPF. Model, View, ViewModel


I am working on a Sticky Notes project and doing the UI in WPF and obviously resorting to MVVM as my design choice for architecture. I am having second thoughts on what should be my Model, View and ViewModel.

我正在开发一个Sticky Notes项目并在WPF中执行UI,显然是将MVVM作为我的架构设计选择。我对应该是什么样的Model,View和ViewModel有了第二个想法。

I have one class that is called Note, here is how it looks like:

我有一个名为Note的类,这是它的样子:

class Note
{
    public Guid ID { get; set; }
    public string Note { get; set; }
}

And I also have User, which stores collection of Notes:

我也有User,它存储了Notes的集合:

public class User
{
    public Guid ID { get; set; }
    public Dictionary<Guid, Note> Notes = new Dictionary<Guid,Note>();
}

So now I need to make my Model and ViewModel. First I was thinking to go with the most obvious approach, which is the Note itself is the Model, then have a NoteViewModel for the ViewModel. But then I thought, what if I make User as the model and have a UserViewModel class for the ViewModel. And if I do so, how do I implement INotifyPropertyChanged. If my model was Note, INotifyPropertyChanged implementation is straightforward. Your thoughts on this will be greatly appreciated.

所以现在我需要制作我的Model和ViewModel。首先,我想要采用最明显的方法,即Note本身就是Model,然后为ViewModel提供NoteViewModel。但后来我想,如果我将User作为模型并拥有ViewModel的UserViewModel类会怎样。如果我这样做,我该如何实现INotifyPropertyChanged。如果我的模型是Note,则INotifyPropertyChanged实现很简单。非常感谢您对此的看法。

2 个解决方案

#1


0  

I think you need to broaden you idea of a model. To put it simply: The model is the representation of the "objects" you will be working with (which can be a database with tables or POCO's like you have defined). The User and Note are both potentially part of the model in the same way that a client table and clientOrders table are part of the model in a database. The ViewModel handles the business logic that interacts with the model and exposes that data to the view through wpf property binding.

我认为你需要拓宽你对模型的看法。简单地说:模型是您将要使用的“对象”的表示(可以是具有表格的数据库或像您定义的POCO)。 User和Note都可能是模型的一部分,就像客户端表和clientOrders表是数据库中模型的一部分一样。 ViewModel处理与模型交互的业务逻辑,并通过wpf属性绑定将该数据公开给视图。

As for INotifyPropertCHanged, here is a simple use (vb):

至于INotifyPropertCHanged,这里有一个简单的用法(vb):

Imports System.ComponentModel

Public Property CustomerName() As String 
        Get 
            Return Me.customerNameValue
        End Get 

        Set(ByVal value As String)
            If Not (value = customerNameValue) Then 
                Me.customerNameValue = value
                NotifyPropertyChanged()
            End If 
        End Set 
    End Property

c#:

 using System.ComponentModel

 public string CustomerName
        {
            get
            {
                return this.customerNameValue;
            }

            set
            {
                if (value != this.customerNameValue)
                {
                    this.customerNameValue = value;
                    NotifyPropertyChanged();
                }
            }
        }

hope this helps

希望这可以帮助

#2


0  

A more illustrative way on how to do this is available on YouTube. Bottom line is that the UserViewModel will be the parent view model, and the multiple NoteViewModels will be child view models. The parent view model will be responsible for creating the child view models. Enjoy the video and like the author says - happy coding!

有关如何执行此操作的更具说明性的方法,请访问YouTube。底线是UserViewModel将是父视图模型,而多个NoteViewModel将是子视图模型。父视图模型将负责创建子视图模型。享受视频,就像作者所说 - 快乐的编码!

http://www.youtube.com/watch?v=Dzv8CtUCchY


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/03/11/cb72d9e5fb12d6698a6f5f38e79d9d95.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号