Portable Model Library中的ViewModel支持

问题描述:

我尝试了一个VS 2010项目中的PCL,我想支持WPF(4及更高版本)和Silverlight(4及更高版本)。下面的MS documentation摘录令我感到困惑。Portable Model Library中的ViewModel支持

似乎要说在PCL项目中引用System.Windows,但我不知道该怎么做。

我必须做什么才能在我的PCL项目中使用ICommand和INotifyPropertyChanged?

支持视图模型图案时,你的目标Silverlight和 的Windows Phone 7,您可以在您的 解决方案视图模型模式。实现此模式的类位于Silverlight的 System.Windows.dll程序集中。

系统:当你创建一个可移植类库 项目,目标在本次大会在.NET Framework 4或Xbox 360

,这些类包括以下不支持System.Windows.dll中 组装。 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

.NET框架4也包含这些类,但它们是 实现的 程序集而不是System.Windows.dll。要使用便携式 类库项目中使用这些类,必须引用System.Windows.dll中,而不是在 列出的.NET框架4文档组件

编辑

INotifyPropertyChanged的不可用;下面的代码将无法编译

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

    ... 

} 
+0

您为'打印机控制语言'标记了代码'pcl'。你的意思是“便携式类库”吗? – nvoigt 2013-05-02 14:04:14

+0

@nvoight - 谢谢! – Berryl 2013-05-02 14:37:42

是的,MSDN是混淆了这一点(有没有错误?)

基本上,你什么都没有做!

当您创建PCL项目时,只需选择适当的框架即可。 new pcl project

PCL自动为您管理参考。

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

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

让我们试试!

+0

这是令人困惑的部分 - 我做到了!看我的编辑;我无法访问INotifyPropertyChanged。 – Berryl 2013-05-02 14:48:43

+0

你是否在VS 2010上使用PCL插件(如我)或VS2012以及NativeE支持? – Berryl 2013-05-02 14:49:40

+0

VS 2010与PCL(喜欢你) – Cybermaxs 2013-05-02 15:04:00