c# - WPF 中的便笺项目。模型、 View 、 View 模型

标签 c# .net wpf xaml mvvm

我正在开发一个 Sticky Notes 项目并在 WPF 中做 UI,显然将 MVVM 作为我的架构设计选择。我正在重新考虑我的模型、 View 和 View 模型应该是什么。

我有一个名为 Note 的类,如下所示:

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

我也有用户,它存储笔记的集合:
public class User
{
    public Guid ID { get; set; }
    public Dictionary<Guid, Note> Notes = new Dictionary<Guid,Note>();
}

所以现在我需要制作我的模型和 View 模型。首先,我想采用最明显的方法,即 Note 本身就是 Model,然后为 ViewModel 设置一个 NoteViewModel。但是后来我想,如果我将 User 作为模型并为 ViewModel 提供一个 UserViewModel 类会怎样。如果我这样做了,我该如何实现 INotifyPropertyChanged。如果我的模型是 Note,INotifyPropertyChanged 的​​实现很简单。您对此的想法将不胜感激。

最佳答案

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

至于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();
                }
            }
        }

希望这可以帮助

关于c# - WPF 中的便笺项目。模型、 View 、 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15347025/

相关文章:

wpf - 如何将多个参数传递给 WPF MarkupExtension 中的 params 数组?

wpf - 将 ItemsControl 中的新项目滚动到 View 中

用于为azure blob中的文件生成可共享下载链接的c#代码

c# - 调用基础 REST API

c# - SaveChangesAsync 返回异常而不是 0

c# - 如何在 C# 中修改 XML 文件?

.net - XAML 解析异常

wpf - 如何获得一个TextBox来填充可调整大小的列?

c# - 在某些 XP 机器上首次读取后流关闭

c# - 我如何解决 Nuget DLL Hell - 无论我做什么 VS 都坚持认为 dll 版本与包中的版本不同