Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class library does not recognize CommandManager class

I'm developing WPF applications and I want to reuse my classes that are the same in all those applications so I can add them as a reference.

In my case I have a class for my Commands:

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {

    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

This works perfect in my application, but when I want to make a class library which I just want to add as a reference in my project, visual studio can't build because "CommandManager does not exists in the current context". In my usings I have the following (which should be enough)

 using System;
 using System.Windows.Input;

Any ideas why I can't do this in a "class library project" ?

like image 887
koala Avatar asked Jun 05 '13 22:06

koala


1 Answers

Go to the "References" part of your class library and select "Add Reference". Look for an assembly called "PresentationCore" and add it.

Then in your class file add the using statement using System.Windows.Input;

You will then be able to access the CommandManager as you expect.

Just adding: lots of guys when they go to create a class library, they select "WPF Custom Control Library" and then erase the "Class1.cs" file. It's a shortcut that automatically adds the right namespaces to your library. Whether it's a good or bad shortcut is anybody's call, but I use it all the time.

like image 113
Gayot Fow Avatar answered Oct 22 '22 04:10

Gayot Fow