gpt4 book ai didi

wpf - 可移植类库中的 ViewModel 支持

转载 作者:行者123 更新时间:2023-12-04 15:45:34 25 4
gpt4 key购买 nike

我正在 VS 2010 项目中试用 PCL,我希望在该项目中支持 WPF(4 及更高版本)和 Silverlight(4 及更高版本)。 MS documentation 下面的摘录让我感到困惑。

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

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

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



编辑

INotifyPropertyChanged 不可用;下面的代码不会编译
    public abstract class ViewModelBase : INotifyPropertyChanged
{
public virtual event PropertyChangedEventHandler PropertyChanged;

...

}

最佳答案

是的,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));
}
}
}

咱们试试吧 !

关于wpf - 可移植类库中的 ViewModel 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16340080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com