ViewModel support in Portable Class Library

I am trying out the PCL in a VS 2010 project in which I would like to support WPF (4 and higher) and Silverlight (4 and higher). The MS documentation excerpt below is confusing to me.

It seems to be saying to reference System.Windows in the PCL project, but I don't see how to do that.

What must I do to have ICommand and INotifyPropertyChanged in my PCL project?

Supporting the View Model Pattern When you target Silverlight and Windows Phone 7, you can implement the view model pattern in your solution. The classes to implement this pattern are located in the System.Windows.dll assembly from Silverlight. The System.Windows.dll assembly is not supported when you create a Portable Class Library project that targets the .NET Framework 4 or Xbox 360.

The classes in this assembly include the following:

System.Collections.ObjectModel.ObservableCollection

System.Collections.ObjectModel.ReadOnlyObservableCollection

System.Collections.Specialized.INotifyCollectionChanged

System.Collections.Specialized.NotifyCollectionChangedAction

System.Collections.Specialized.NotifyCollectionChangedEventArgs

System.Collections.Specialized.NotifyCollectionChangedEventHandler

System.Windows.Input.ICommand

The .NET Framework 4 also contains these classes, but they are implemented in assemblies other than System.Windows.dll. To use these classes with a Portable Class Library project, you must reference System.Windows.dll and not the assemblies listed in the .NET Framework 4 documentation

EDIT

INotifyPropertyChanged is NOT available; the code below will not compile

    public abstract class ViewModelBase : INotifyPropertyChanged
{
    public virtual event PropertyChangedEventHandler PropertyChanged;

    ...

}

Yes, the MSDN is confusing on this point (is there an error ?)

Basically, you have nothing to do !

Whe you create your PCL project, simply select appropriate frameworks. 新的pcl项目

PCL automatically manage references for you.

 public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }

Let's try !

链接地址: http://www.djcxy.com/p/68856.html

上一篇: 可移植类库与库项目

下一篇: 可移植类库中的ViewModel支持