ICommand. What am I doing wrong?

S

shapper

Hello,

I am not sure if this question is for this forum. It is a WPF C#
question.

I implemented a ICommand interface as follows:

public class SetupCommand : ICommand {

Boolean CanExecute(Object parameter) {
return true;
} // CanExecute

void Execute(Object parameter) {
Setup setup = new Setup();
setup.Run();
} // Execute

} // SetupCommand

On my WPF window I am adding a menu at runtime:

Menu menu = new Menu();
menu.Items.Add(new MenuItem { Command = SetupCommand, Header =
"Setup" });

In Command of the menu item it says:
System.Windows.Input.ICommand MenuItem Command
Gets or sets the command associated to the menu item. This is a
dependency property.

I get the following error:
'SetupCommand' is a 'type' but is used like a 'variable'.

I also tried to wrap the SetupCommand in a MenuCommands class and
expose it through a property:

public class MenuCommands {

public ICommand Setup { get { return new SetupCommand(); } }

public class SetupCommand : ICommand {

Boolean CanExecute(Object parameter) {
return true;
} // CanExecute

void Execute(Object parameter) {
//Setup setup = new Setup();
//setup.Run();
} // Execute

} // SetupCommand

}

And on menu item I try:
menu.Items.Add(new MenuItem { Command = MenuCommands.Setup, Header =
"Setup" });

This does not work either.

It works if I use something like:
menu.Items.Add(new MenuItem { Command = ApplicationCommands.Close,
Header = "Close" });

ApplicationCommands is defined in System.Windows.Input.

What am I doing wrong in my implementation?

Thanks.
Miguel
 
S

shapper

Hello,

I am not sure if this question is for this forum. It is a WPF C#
question.

I implemented a ICommand interface as follows:

    public class SetupCommand : ICommand {

      Boolean CanExecute(Object parameter) {
        return true;
      } // CanExecute

      void Execute(Object parameter) {
        Setup setup = new Setup();
        setup.Run();
      } // Execute

    } // SetupCommand

On my WPF window I am adding a menu at runtime:

      Menu menu = new Menu();
      menu.Items.Add(new MenuItem { Command = SetupCommand, Header =
"Setup" });

In Command of the menu item it says:
System.Windows.Input.ICommand    MenuItem Command
Gets or sets the command associated to the menu item. This is a
dependency property.

I get the following error:
'SetupCommand' is a 'type' but is used like a 'variable'.

I also tried to wrap the SetupCommand in a MenuCommands class and
expose it through a property:

    public class MenuCommands {

      public ICommand Setup { get { return new SetupCommand(); } }

      public class SetupCommand : ICommand {

        Boolean CanExecute(Object parameter) {
          return true;
        } // CanExecute

        void Execute(Object parameter) {
          //Setup setup = new Setup();
          //setup.Run();
        } // Execute

      } // SetupCommand

    }

And on menu item I try:
menu.Items.Add(new MenuItem { Command = MenuCommands.Setup, Header =
"Setup" });

This does not work either.

It works if I use something like:
menu.Items.Add(new MenuItem { Command = ApplicationCommands.Close,
Header = "Close" });

ApplicationCommands is defined in System.Windows.Input.

What am I doing wrong in my implementation?

Thanks.
Miguel

Hello,

I just solved it. I needed to define the wrapper class as static and
the property to.

Thanks,
Miguel
 
S

shapper

Hello,

I just solved it. I needed to define the wrapper class as static and
the property to.

Thanks,
Miguel

Hi,

I am using the TextWriterProxy in the WPF application as follows:

TextBlock output = new TextBlock();

TextWriterProxy proxy = new TextWriterProxy();
proxy.Add(Console.Out);

StringBuilder sb = new StringBuilder();
StringWriter resultStringWriter = new StringWriter(sb);
proxy.Add(resultStringWriter);
output.Text = sb.ToString();

However, text block does not show any text. Any idea how to do this
integration?

The TextWriterProxy code is as follows:


public class TextWriterProxy : TextWriter {

private List<TextWriter> _writers = new List<TextWriter>();

public override Encoding Encoding { get { return
Encoding.Default; } } // Encoding

public override string NewLine {
get { return base.NewLine; }
set {
foreach (TextWriter tw in _writers)
tw.NewLine = value;
base.NewLine = value;
}
} // NewLine

public void Add(TextWriter writer) {
if (!_writers.Contains(writer))
_writers.Add(writer);
} // Add

public bool Remove(TextWriter writer) {
return _writers.Remove(writer);
} // Remove

public override void Write(char value) {
foreach (TextWriter tw in _writers)
tw.Write(value);
base.Write(value);
} // Write

public override void Close() {
foreach (TextWriter tw in _writers)
tw.Close();
base.Close();
} // Close

protected override void Dispose(bool disposing) {
foreach (TextWriter tw in _writers)
tw.Dispose();
base.Dispose(disposing);
} // Dispose

public override void Flush() {
foreach (TextWriter tw in _writers)
tw.Flush();
base.Flush();
} // Flush

} // TextWriterProxy

Thanks,
Miguel
 
S

shapper

Hi,

I am using the TextWriterProxy in the WPF application as follows:

      TextBlock output = new TextBlock();

      TextWriterProxy proxy = new TextWriterProxy();
      proxy.Add(Console.Out);

      StringBuilder sb = new StringBuilder();
      StringWriter resultStringWriter = new StringWriter(sb);
      proxy.Add(resultStringWriter);
      output.Text = sb.ToString();

However, text block does not show any text. Any idea how to do this
integration?

The TextWriterProxy code is as follows:

  public class TextWriterProxy : TextWriter {

    private List<TextWriter> _writers = new List<TextWriter>();

    public override Encoding Encoding { get { return
Encoding.Default; } }  // Encoding

    public override string NewLine {
      get { return base.NewLine; }
      set {
        foreach (TextWriter tw in _writers)
          tw.NewLine = value;
        base.NewLine = value;
      }
    } // NewLine

    public void Add(TextWriter writer) {
      if (!_writers.Contains(writer))
        _writers.Add(writer);
    } // Add

    public bool Remove(TextWriter writer) {
      return _writers.Remove(writer);
    } // Remove

    public override void Write(char value) {
      foreach (TextWriter tw in _writers)
        tw.Write(value);
      base.Write(value);
    } // Write

    public override void Close() {
      foreach (TextWriter tw in _writers)
        tw.Close();
      base.Close();
    } // Close

    protected override void Dispose(bool disposing) {
      foreach (TextWriter tw in _writers)
        tw.Dispose();
      base.Dispose(disposing);
    } // Dispose

    public override void Flush() {
      foreach (TextWriter tw in _writers)
        tw.Flush();
      base.Flush();
    } // Flush

  } // TextWriterProxy

Thanks,
Miguel

Sorry, wrong thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top