了解 MVVM 模式
Understanding MVVM Pattern
我正在尝试了解 MVVM 模式。我正在学习教程 here。我支持示例 4,框架。至少对于 observable class,放置代码对我来说是个问题。我在我的项目中创建了一个名为 HelperClass 的新文件夹,并复制并粘贴了 observable class.
Observable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
namespace Morza.HelperClass
{
[Serializable]
public abstract class ObservableObject : INotifyPropertyChanged
{
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged(String propertyName)
{
VerifyPropertyName(propertyName);
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Warns the developer if this Object does not have a public property with
/// the specified name. This method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(String propertyName)
{
// verify that the property name matches a real,
// public, instance property on this Object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
Debug.Fail("Invalid property name: " + propertyName);
}
}
}
}
但是PropertySupport说它在当前概念中不存在。 Morza 是项目名称。
我的第二个问题:
如果我想实现observable,是否需要实现两个ViewModel,其中
如果我有一个人,我会做以下事情
Model
Person
ViewModel
PersonViewModel
PersonListViewModel
PersonListViewModel 哪里会有 PersonViewModel 的可观察列表?
PropertySupport
不是 .NET 的一部分。如果您使用的是框架教程,请确保在您的项目中包含该框架。
标准的 INotifyPropertyChanged 实现如下所示:
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
来自 MSDN.
要回答你的第二个问题,你的整个 view 应该有一个视图模型。该视图模型将包含您的 Person
对象列表。如果你想制作单独的 Person
模型和视图模型对象,那么它将包含一个 PersonViewModel
集合。然而,这种分离通常是不必要的。
PersonListViewModel
不太可能需要。就名字而言,这听起来像是您不想拥有的 class。
我正在尝试了解 MVVM 模式。我正在学习教程 here。我支持示例 4,框架。至少对于 observable class,放置代码对我来说是个问题。我在我的项目中创建了一个名为 HelperClass 的新文件夹,并复制并粘贴了 observable class.
Observable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
namespace Morza.HelperClass
{
[Serializable]
public abstract class ObservableObject : INotifyPropertyChanged
{
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged(String propertyName)
{
VerifyPropertyName(propertyName);
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Warns the developer if this Object does not have a public property with
/// the specified name. This method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(String propertyName)
{
// verify that the property name matches a real,
// public, instance property on this Object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
Debug.Fail("Invalid property name: " + propertyName);
}
}
}
}
但是PropertySupport说它在当前概念中不存在。 Morza 是项目名称。
我的第二个问题:
如果我想实现observable,是否需要实现两个ViewModel,其中
如果我有一个人,我会做以下事情
Model
Person
ViewModel
PersonViewModel
PersonListViewModel
PersonListViewModel 哪里会有 PersonViewModel 的可观察列表?
PropertySupport
不是 .NET 的一部分。如果您使用的是框架教程,请确保在您的项目中包含该框架。
标准的 INotifyPropertyChanged 实现如下所示:
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
来自 MSDN.
要回答你的第二个问题,你的整个 view 应该有一个视图模型。该视图模型将包含您的 Person
对象列表。如果你想制作单独的 Person
模型和视图模型对象,那么它将包含一个 PersonViewModel
集合。然而,这种分离通常是不必要的。
PersonListViewModel
不太可能需要。就名字而言,这听起来像是您不想拥有的 class。