ICommand. What am I doing wrong?

  • Follow


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





0
Reply shapper 11/20/2009 12:41:43 PM

On Nov 20, 12:41=A0pm, shapper <mdmo...@gmail.com> wrote:
> 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:
>
> =A0 =A0 public class SetupCommand : ICommand {
>
> =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> =A0 =A0 =A0 =A0 return true;
> =A0 =A0 =A0 } // CanExecute
>
> =A0 =A0 =A0 void Execute(Object parameter) {
> =A0 =A0 =A0 =A0 Setup setup =3D new Setup();
> =A0 =A0 =A0 =A0 setup.Run();
> =A0 =A0 =A0 } // Execute
>
> =A0 =A0 } // SetupCommand
>
> On my WPF window I am adding a menu at runtime:
>
> =A0 =A0 =A0 Menu menu =3D new Menu();
> =A0 =A0 =A0 menu.Items.Add(new MenuItem { Command =3D SetupCommand, Heade=
r =3D
> "Setup" });
>
> In Command of the menu item it says:
> System.Windows.Input.ICommand =A0 =A0MenuItem 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:
>
> =A0 =A0 public class MenuCommands {
>
> =A0 =A0 =A0 public ICommand Setup { get { return new SetupCommand(); } }
>
> =A0 =A0 =A0 public class SetupCommand : ICommand {
>
> =A0 =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> =A0 =A0 =A0 =A0 =A0 return true;
> =A0 =A0 =A0 =A0 } // CanExecute
>
> =A0 =A0 =A0 =A0 void Execute(Object parameter) {
> =A0 =A0 =A0 =A0 =A0 //Setup setup =3D new Setup();
> =A0 =A0 =A0 =A0 =A0 //setup.Run();
> =A0 =A0 =A0 =A0 } // Execute
>
> =A0 =A0 =A0 } // SetupCommand
>
> =A0 =A0 }
>
> And on menu item I try:
> menu.Items.Add(new MenuItem { Command =3D MenuCommands.Setup, Header =3D
> "Setup" });
>
> This does not work either.
>
> It works if I use something like:
> menu.Items.Add(new MenuItem { Command =3D ApplicationCommands.Close,
> Header =3D "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
0
Reply shapper 11/20/2009 12:50:53 PM


On Nov 20, 12:50=A0pm, shapper <mdmo...@gmail.com> wrote:
> On Nov 20, 12:41=A0pm, shapper <mdmo...@gmail.com> wrote:
>
>
>
> > 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:
>
> > =A0 =A0 public class SetupCommand : ICommand {
>
> > =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> > =A0 =A0 =A0 =A0 return true;
> > =A0 =A0 =A0 } // CanExecute
>
> > =A0 =A0 =A0 void Execute(Object parameter) {
> > =A0 =A0 =A0 =A0 Setup setup =3D new Setup();
> > =A0 =A0 =A0 =A0 setup.Run();
> > =A0 =A0 =A0 } // Execute
>
> > =A0 =A0 } // SetupCommand
>
> > On my WPF window I am adding a menu at runtime:
>
> > =A0 =A0 =A0 Menu menu =3D new Menu();
> > =A0 =A0 =A0 menu.Items.Add(new MenuItem { Command =3D SetupCommand, Hea=
der =3D
> > "Setup" });
>
> > In Command of the menu item it says:
> > System.Windows.Input.ICommand =A0 =A0MenuItem 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:
>
> > =A0 =A0 public class MenuCommands {
>
> > =A0 =A0 =A0 public ICommand Setup { get { return new SetupCommand(); } =
}
>
> > =A0 =A0 =A0 public class SetupCommand : ICommand {
>
> > =A0 =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> > =A0 =A0 =A0 =A0 =A0 return true;
> > =A0 =A0 =A0 =A0 } // CanExecute
>
> > =A0 =A0 =A0 =A0 void Execute(Object parameter) {
> > =A0 =A0 =A0 =A0 =A0 //Setup setup =3D new Setup();
> > =A0 =A0 =A0 =A0 =A0 //setup.Run();
> > =A0 =A0 =A0 =A0 } // Execute
>
> > =A0 =A0 =A0 } // SetupCommand
>
> > =A0 =A0 }
>
> > And on menu item I try:
> > menu.Items.Add(new MenuItem { Command =3D MenuCommands.Setup, Header =
=3D
> > "Setup" });
>
> > This does not work either.
>
> > It works if I use something like:
> > menu.Items.Add(new MenuItem { Command =3D ApplicationCommands.Close,
> > Header =3D "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

Hi,

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

      TextBlock output =3D new TextBlock();

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

      StringBuilder sb =3D new StringBuilder();
      StringWriter resultStringWriter =3D new StringWriter(sb);
      proxy.Add(resultStringWriter);
      output.Text =3D 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 =3D 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 =3D value;
        base.NewLine =3D 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
0
Reply shapper 11/20/2009 5:06:19 PM

On Nov 20, 5:06=A0pm, shapper <mdmo...@gmail.com> wrote:
> On Nov 20, 12:50=A0pm, shapper <mdmo...@gmail.com> wrote:
>
>
>
> > On Nov 20, 12:41=A0pm, shapper <mdmo...@gmail.com> wrote:
>
> > > 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:
>
> > > =A0 =A0 public class SetupCommand : ICommand {
>
> > > =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> > > =A0 =A0 =A0 =A0 return true;
> > > =A0 =A0 =A0 } // CanExecute
>
> > > =A0 =A0 =A0 void Execute(Object parameter) {
> > > =A0 =A0 =A0 =A0 Setup setup =3D new Setup();
> > > =A0 =A0 =A0 =A0 setup.Run();
> > > =A0 =A0 =A0 } // Execute
>
> > > =A0 =A0 } // SetupCommand
>
> > > On my WPF window I am adding a menu at runtime:
>
> > > =A0 =A0 =A0 Menu menu =3D new Menu();
> > > =A0 =A0 =A0 menu.Items.Add(new MenuItem { Command =3D SetupCommand, H=
eader =3D
> > > "Setup" });
>
> > > In Command of the menu item it says:
> > > System.Windows.Input.ICommand =A0 =A0MenuItem 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:
>
> > > =A0 =A0 public class MenuCommands {
>
> > > =A0 =A0 =A0 public ICommand Setup { get { return new SetupCommand(); =
} }
>
> > > =A0 =A0 =A0 public class SetupCommand : ICommand {
>
> > > =A0 =A0 =A0 =A0 Boolean CanExecute(Object parameter) {
> > > =A0 =A0 =A0 =A0 =A0 return true;
> > > =A0 =A0 =A0 =A0 } // CanExecute
>
> > > =A0 =A0 =A0 =A0 void Execute(Object parameter) {
> > > =A0 =A0 =A0 =A0 =A0 //Setup setup =3D new Setup();
> > > =A0 =A0 =A0 =A0 =A0 //setup.Run();
> > > =A0 =A0 =A0 =A0 } // Execute
>
> > > =A0 =A0 =A0 } // SetupCommand
>
> > > =A0 =A0 }
>
> > > And on menu item I try:
> > > menu.Items.Add(new MenuItem { Command =3D MenuCommands.Setup, Header =
=3D
> > > "Setup" });
>
> > > This does not work either.
>
> > > It works if I use something like:
> > > menu.Items.Add(new MenuItem { Command =3D ApplicationCommands.Close,
> > > Header =3D "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
>
> Hi,
>
> I am using the TextWriterProxy in the WPF application as follows:
>
> =A0 =A0 =A0 TextBlock output =3D new TextBlock();
>
> =A0 =A0 =A0 TextWriterProxy proxy =3D new TextWriterProxy();
> =A0 =A0 =A0 proxy.Add(Console.Out);
>
> =A0 =A0 =A0 StringBuilder sb =3D new StringBuilder();
> =A0 =A0 =A0 StringWriter resultStringWriter =3D new StringWriter(sb);
> =A0 =A0 =A0 proxy.Add(resultStringWriter);
> =A0 =A0 =A0 output.Text =3D sb.ToString();
>
> However, text block does not show any text. Any idea how to do this
> integration?
>
> The TextWriterProxy code is as follows:
>
> =A0 public class TextWriterProxy : TextWriter {
>
> =A0 =A0 private List<TextWriter> _writers =3D new List<TextWriter>();
>
> =A0 =A0 public override Encoding Encoding { get { return
> Encoding.Default; } } =A0// Encoding
>
> =A0 =A0 public override string NewLine {
> =A0 =A0 =A0 get { return base.NewLine; }
> =A0 =A0 =A0 set {
> =A0 =A0 =A0 =A0 foreach (TextWriter tw in _writers)
> =A0 =A0 =A0 =A0 =A0 tw.NewLine =3D value;
> =A0 =A0 =A0 =A0 base.NewLine =3D value;
> =A0 =A0 =A0 }
> =A0 =A0 } // NewLine
>
> =A0 =A0 public void Add(TextWriter writer) {
> =A0 =A0 =A0 if (!_writers.Contains(writer))
> =A0 =A0 =A0 =A0 _writers.Add(writer);
> =A0 =A0 } // Add
>
> =A0 =A0 public bool Remove(TextWriter writer) {
> =A0 =A0 =A0 return _writers.Remove(writer);
> =A0 =A0 } // Remove
>
> =A0 =A0 public override void Write(char value) {
> =A0 =A0 =A0 foreach (TextWriter tw in _writers)
> =A0 =A0 =A0 =A0 tw.Write(value);
> =A0 =A0 =A0 base.Write(value);
> =A0 =A0 } // Write
>
> =A0 =A0 public override void Close() {
> =A0 =A0 =A0 foreach (TextWriter tw in _writers)
> =A0 =A0 =A0 =A0 tw.Close();
> =A0 =A0 =A0 base.Close();
> =A0 =A0 } // Close
>
> =A0 =A0 protected override void Dispose(bool disposing) {
> =A0 =A0 =A0 foreach (TextWriter tw in _writers)
> =A0 =A0 =A0 =A0 tw.Dispose();
> =A0 =A0 =A0 base.Dispose(disposing);
> =A0 =A0 } // Dispose
>
> =A0 =A0 public override void Flush() {
> =A0 =A0 =A0 foreach (TextWriter tw in _writers)
> =A0 =A0 =A0 =A0 tw.Flush();
> =A0 =A0 =A0 base.Flush();
> =A0 =A0 } // Flush
>
> =A0 } // TextWriterProxy
>
> Thanks,
> Miguel

Sorry, wrong thread.
0
Reply shapper 11/20/2009 5:07:48 PM

3 Replies
253 Views

(page loaded in 0.475 seconds)


Reply: