Reputation: 1934
I am trying to understand the MVVM pattern. I am following the tutorial here. I am stock at Example 4, framework. Putting the code cause issue for me at least for the observable class. I created a new folder called it HelperClass inside my project and copy and pasted the 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);
}
}
}
}
However PropertySupport say it does not exist in the current concept. Morza is the project name.
My second questions:
If I want to implement observable, do I need to implement two ViewModel, where
If I have a person I would do the following
Model
Person
ViewModel
PersonViewModel
PersonListViewModel
Where PersonListViewModel would have an observable list of PersonViewModel?
Upvotes: 0
Views: 130
Reputation: 61349
PropertySupport
is not part of .NET. If you are using a framework tutorial, make sure to include that framework in your project.
The standard INotifyPropertyChanged implementation looks like this:
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));
}
}
From MSDN.
To answer your second question, your whole view should have a view model. That view model would then contain your list of Person
objects. If you want to make separate Person
model and view model objects, then it would contain a PersonViewModel
collection instead. This separation is often unnecessary however.
PersonListViewModel
is very unlikely to be needed. Just by the name, it sounds like a class you do not want to have.
Upvotes: 1