1

I was primarily following this tutorial: http://www.codeproject.com/Articles/238657/How-to-use-Commands-in-WPF

But then I realize the RelayCommand is part of another framework that I can't use. This is the code I have:

    public ICommand TestCommand
    {
        get;
        internal set;
    }

    private bool CanExecuteTestCommand()
    {
        return !string.IsNullOrEmpty(txtUsername);
    }

    private void CreateTestCommand()
    {
        TestCommand = new TestCommand(TestExecute, CanExecuteTestCommand);
    }

    public void TestExecute(object parameter)
    {
        obj.TestConnection();
    }

And the XAML:

<Button Content="Test Connection" Command="{Binding Path=TestConCmd}" />

But this won't compile because TestCommand is, obviously, an invalid type.

I've looked over this tutorial as well:

http://www.codeproject.com/Articles/274982/Commands-in-MVVM

But similarly, Command doesn't seem to be a type even though I've added using System.Windows.Input.

Then all the other tutorials I've looked at just use built-in commands like closing the application, pasting from the clipboard and a few other things like that.

So... How do I actually create my command?

sab669
  • 3,574
  • 4
  • 29
  • 64
  • You could just go to the link in this article and download the source of MVVM Light Toolkit to see it's implementation. http://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight (PCL)/Command/RelayCommand.cs. But a word of warning: Trying to start with MVVM without a proper and matured MVVM Framework will be a pain in the ass and you'll do yourself a favor by using one of the matured frameworks in your projects if you don't want to reinvent every single thing – Tseng Oct 02 '15 at 23:00
  • @Tseng Due to outrageous security policies at work I can't download almost anything. Also, it's an extremely simple app that I'm re-writing from C++. It's just a single form that processes a bunch of text files and writes some stuff to a database. Internal use only. Nothing too demanding or requiring of a "matured framework", really. Mostly just taking this as an opportunity to learn new stuff. – sab669 Oct 02 '15 at 23:03

1 Answers1

0

Command is not a type, ICommand is. You must derive from/implement it:

public class TestCommand : ICommand
{
    public override void Execute(object parameter)
    {
       //Do stuff
    }
}

And subsequently implement the methods, especially Execute(object parameter). Then you can do:

TestCommand = new TestCommand();

in your View Model as before. Of course, you can re-implement RelayCommand or something like it. Is Josh Smith's implementation of the RelayCommand flawed? Shows some code and easy to make mistakes.

Community
  • 1
  • 1
BradleyDotNET
  • 57,599
  • 10
  • 90
  • 109
  • So then does this `TestCommand` class need an instance of my actual model so that it can call TestConnection on that object? Seems kind of weird, to me. – sab669 Oct 02 '15 at 19:58
  • @sab669 Possibly, if you don't do a generic `Delegate/RelayCommand` then you would have to pass a `TestConnection` object for it to act on to it in your example. If you *do* pass a delegate to it, of course the assigned method could have access to that variable. – BradleyDotNET Oct 02 '15 at 20:00