我正在执行我在 Expression Blend 4 中找到的使用 WPF 连接到 SQL Server 的教程。在我进行构建时,在 VS12 中的最后步骤之后,我收到以下错误。
Error 1 The type or namespace name 'DelegateCommand' could not be found (are you missing a using directive or an assembly reference?)
Error 2 The type or namespace name 'DelegateCommand' could not be found (are you missing a using directive or an assembly reference?)
当我进行清洁时,我没有收到这些错误。 我的目标是 .net 4.5 我也试过 4.0
我的错误代码看起来像这样。我将两条错误线加粗。这是一个名为 Class1.cs 的文件
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace AWADataSource
{
public class ProductPhotosCollection
{
**private DelegateCommand getDataCommand;
public DelegateCommand GetDataCommand { get { return getDataCommand; } }**
public ProductPhotosCollection()
{
getDataCommand = new DelegateCommand(delegate() { GetData(); });
}
public ObservableCollection<ProductPhoto> ProductPhotos
{ get { return this.productPhotos; } }
private ObservableCollection<ProductPhoto> productPhotos =
new ObservableCollection<ProductPhoto>();
private void GetData()
{
ProductPhotosTableAdapters.ProductPhotoTableAdapter da =
new ProductPhotosTableAdapters.ProductPhotoTableAdapter();
ProductPhotos.ProductPhotoDataTable dt = da.GetData();
productPhotos.Clear();
foreach (ProductPhotos.ProductPhotoRow row in dt)
{
productPhotos.Add(new ProductPhoto(
row.ProductPhotoID,
row.ThumbNailPhoto,
row.LargePhoto,
row.ModifiedDate));
}
}
}
public class ProductPhoto
{
// Public Accessors to the private properties.
public int ID { get { return id; } }
public ImageSource ThumbNailPhoto { get { return thumbNailPhoto; } }
public ImageSource LargePhoto { get { return largePhoto; } }
public DateTime ModifiedDate { get { return modifiedDate; } }
// Constructor.
public ProductPhoto(int id, byte[] thumbNailPhoto, byte[] largePhoto,
DateTime modifiedDate)
{
this.id = id;
this.thumbNailPhoto = ByteArrayToImageSource(thumbNailPhoto);
this.largePhoto = ByteArrayToImageSource(largePhoto);
this.modifiedDate = modifiedDate;
}
// Private properties.
private int id;
private ImageSource thumbNailPhoto;
private ImageSource largePhoto;
private DateTime modifiedDate;
// Supporting method.
private ImageSource ByteArrayToImageSource(byte[] data)
{
BitmapImage image = null;
if (null != data)
{
image = new BitmapImage();
image.BeginInit();
image.StreamSource = new System.IO.MemoryStream(data);
image.EndInit();
}
return image;
}
}
}
我的另一个文件叫做 DelegateCommand.cs,它几乎是一个复制和粘贴。
namespace AWDataSource
{
using System;
using System.Windows.Input;
///
/// DelegateCommand is a simplified version of ICommand in WPF. You can wrap one of these around any method,
/// and thus bind any command on any WPF object to your method.
///
/// DelegateCommand also supports an IsEnabled property that you can use to turn the command on and off.
///
public sealed class DelegateCommand : ICommand
{
// Remember the method so that it can be called at the right time.
private SimpleEventHandler handler;
// Maintain the enabled state.
private bool isEnabled = true;
// Type signature of the method that DelegateCommand works with - returns void, no arguments.
public delegate void SimpleEventHandler();
// Simple constructor: Pass in the method that needs to be called when the command executes.
public DelegateCommand(SimpleEventHandler handler)
{
this.handler = handler;
}
#region ICommand implementation
// Executing the command is as simple as calling the method.
void ICommand.Execute(object arg)
{
this.handler();
}
// Saying whether the command can be executed.
bool ICommand.CanExecute(object arg)
{
return this.IsEnabled;
}
// This is the event that the command architecture of WPF listens to so it knows when to update
// the UI on command enable/disable.
public event EventHandler CanExecuteChanged;
#endregion
// Public visibility of the isEnabled flag - note that when it is set, the event must be raised
// so that WPF knows to update any UI that uses this command.
public bool IsEnabled
{
get { return this.isEnabled; }
set
{
this.isEnabled = value;
this.OnCanExecuteChanged();
}
}
// Simple event propagation that makes sure that someone is listening to the event before raising it.
private void OnCanExecuteChanged()
{
if (this.CanExecuteChanged != null)
{
this.CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}