Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ICommand. What am I doing wrong?

2 views
Skip to first unread message

shapper

unread,
Nov 20, 2009, 7:41:43 AM11/20/09
to
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

shapper

unread,
Nov 20, 2009, 7:50:53 AM11/20/09
to

Hello,

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

Thanks,
Miguel

shapper

unread,
Nov 20, 2009, 12:06:19 PM11/20/09
to

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

shapper

unread,
Nov 20, 2009, 12:07:48 PM11/20/09
to

Sorry, wrong thread.

0 new messages