[WPF]-commandhelper class for WPF/Silverlight

Source: Internet
Author: User

 

 

A piece posted by WPF/Silverlight expert nick on his blogArticleFor the following classes written by the command of WPF and Silverlight, I hope to help you.

Address: http://blogs.msdn.com/nickkramer/archive/2009/01/15/command-helper-classes-for-silverlight-wpf.aspx

here's a couple classes I 've found helpful when writing WPF & Silverlight applications, which I 've named simply command and commandhelper. sometimes, you just want to do simple commanding stuff, and you don't need the overhead of routedcommand. (Also, Silverlight doesn't have routedcommand) The command class below gives you a quick & easy way to define commands and ways to invoke them: keyboard shortcuts, context menus, toolbars, etc. sample usage:

command = new command ();
command. TEXT = "zoom";
command. key = key. z;
command. modifierkeys = modifierkeys. control;
command. button = zoombutton;
command. execute + = delegate () {
imagedisplay. zoom =! Imagedisplay. Zoom;
};
commands. addcommand (command); // call commandhelper. addcommand

As a bonus, by avoiding WPF keybinding, you can tie your commands to keystrokes that WPF's keybinding wouldn't let you, such as "a" w/o CTRL or Alt modifiers.

Here's the complete code:

Public class command: icommand
{
Public event simpledelegate execute;
// Public event canceleventhandler canexecute;

Void icommand. Execute (object parameter)
{
If (Execute! = NULL)
Execute ();
}

bool icommand. canexecute (object parameter)
{< br> // not necessary for this application, and canceleventargs doesn't exist on Silverlight
// canceleventargs ARGs = new canceleventargs (false);
// If (canexecute! = NULL)
// canexecute (this, argS);
// return! Args. Cancel;
return true;
}

Event eventhandler icommand. canexecutechanged
{
Add {}
Remove {}
}

Public Key key = key. None;
Public String displaykey;
Public modifierkeys = modifierkeys. None;
Public String text = "";
Public bool hasmenuitem = true;
Public button = NULL; // hooks up the command to the button
}

Public class commandhelper
{
Private uielement owner;
Private list <command> commands = new list <command> ();

Public commandhelper (uielement owner)
{
This. Owner = owner;
Owner. keydown + = new keyeventhandler (keydown );
}

Private void keydown (Object sender, keyeventargs E)
{
Foreach (command in commands ){
// Intentionally ignore modifier keys
Bool shiftkeymatches = (command. modifierkeys & modifierkeys. Shift) = (keyboard. modifiers & modifierkeys. Shift );
If (command. Key = E. Key & shiftkeymatches ){
(Command as icommand). Execute (null );
}
}
}

 

# If WPF
Public void addbinding (command, routedcommand applicationcommand)
{< br> commandbinding binding = new commandbinding (applicationcommand);
binding. executed + = delegate (Object sender, executedroutedeventargs e)
{< br> (icommand) command ). execute (null);
};
owner. commandbindings. add (binding);
}

Public contextmenu;
# Endif

Public void addmenuseparator ()
{
# If WPF
VaR item = new separator ();
Contextmenu. Items. Add (item );
# Endif
}

Public void addcommand (command)
{
Commands. Add (command );

// keybinding insists that modifierkeys! = 0 for alphabetic keys,
// so we have to roll our own
// This. commandbindings. add (New commandbinding (command);
// keygesture gesture = new keygesture (command. key, command. modifierkeys);
// This. inputbindings. add (New keybinding (command, gesture);

# If WPF
If (command. hasmenuitem ){
Menuitem item = new menuitem ();
String text = command. Text + shortcuttext (command );
Item. header = text;
Item. Command = command;
Contextmenu. Items. Add (item );
}
# Endif

If (command. Button! = NULL ){
String text = command. Text + shortcuttext (command );
Tooltip = new tooltip ();
Tooltip. content = text;
Tooltip. Background = (Brush) application. Current. Resources ["menubackground"];
Tooltip. Foreground = (Brush) application. Current. Resources ["menuforeground"];
Tooltip. borderbrush = (Brush) application. Current. Resources ["shotclockbrush"];
Command. Button. Click + = (Object sender, routedeventargs e) => {
(Command as icommand). Execute (null );
};
# If WPF
Command. Button. tooltip = tooltip;
// Command. Button. Command = command;
# Endif
}
}

Private Static string shortcuttext (command)
{
String text = "";
String keytext = NULL;
If (command. displaykey! = NULL)
Keytext = command. displaykey;
Else if (command. Key! = Key. None ){
Keytext = command. Key. tostring ();
If (command. modifierkeys & modifierkeys. Shift )! = 0)
Keytext = "Shift +" + keytext;
}

If (keytext! = NULL)
Text + = "(" + keytext + ")";
Return text;
}
}

Some assembly required:

    • Inside addcommand when creating Tool tips for toolbar buttons, I put some app-specific styling logic...
    • I punted on supporting canexecute. it's trivial to get working in WPF, just uncomment the code above. silverlight is nontrivial, hard part is deciding when to call it for the toolbar case -- WPF essential polls on a timer with some heuristics to minimize perf cost, you'll need to figure out what heuristics work for your Silverlight app.

enjoy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.