Search code examples
wpfc#-4.0visual-studio-2012expression-blend

Am I OK with a Clean <project Name> in Visual Studio 12?


I am Performing a Tutorial I found in Expression Blend 4 for connecting to a SQL Server with WPF. After the final steps in VS12 when I do a build I get the following error.

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?)

When I do a Clean I do not get these errors. My Target is .net 4.5 I also tried 4.0

My code That is erroring looks Like this. I bolded the two erroring lines. this is a file called 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;
        }
    }
}

and my other file is called DelegateCommand.cs which was pretty much a copy and paist.

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);
        }
        }
    }
}

Solution

  • ProductPhotosCollection is in namespace AWADataSource while DelegateCommand is in AWDataSource.

    Probably a typo, but you either need to put them in the same namespace, or use a using to import the AWDataSource namespace into ProductPhotosCollection (or in your case "Class1.cs")