15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1062 ❘ ChaPTer 36 business ApplicAtiOns with wpf<br />

defining Comm<strong>and</strong>s<br />

.<strong>NET</strong> 4 gives you classes that return predefined comm<strong>and</strong>s. The ApplicationComm<strong>and</strong>s class defines<br />

the static properties New, Open, Close, Print, Cut, Copy, Paste, <strong>and</strong> others. These properties return<br />

RoutedUIComm<strong>and</strong> objects that can be used for a specific purpose. Other classes offering comm<strong>and</strong>s are<br />

NavigationComm<strong>and</strong>s <strong>and</strong> MediaComm<strong>and</strong>s. NavigationComm<strong>and</strong>s gives you comm<strong>and</strong>s that are common<br />

for navigation such as GoToPage, NextPage, <strong>and</strong> PreviousPage. MediaComm<strong>and</strong>s are useful for running a<br />

media player with Play, Pause, Stop, Rewind, <strong>and</strong> Record.<br />

It’s not hard to define custom comm<strong>and</strong>s that fulfill application domain–specific actions. For this, the<br />

BooksComm<strong>and</strong>s class is created that returns a RoutedUIComm<strong>and</strong> with the ShowBooks property. You can<br />

also assign an input gesture to a comm<strong>and</strong>, such as KeyGesture or MouseGesture. Here, a KeyGesture is<br />

assigned that defines the key B with the ALT modifier. An input gesture is a comm<strong>and</strong> source, so clicking<br />

the ALT-B key invokes the comm<strong>and</strong>:<br />

public static class BooksComm<strong>and</strong>s<br />

{<br />

private static RoutedUIComm<strong>and</strong> showBooks;<br />

public static IComm<strong>and</strong> ShowBooks<br />

{<br />

get<br />

{<br />

if (showBooks == null)<br />

{<br />

showBooks = new RoutedUIComm<strong>and</strong>("Show Books", "ShowBooks",<br />

typeof(BooksComm<strong>and</strong>s));<br />

showBook.InputGestures.Add(<br />

new KeyGesture(Key.B, ModifierKeys.Alt));<br />

}<br />

return showBooks;<br />

}<br />

}<br />

defining Comm<strong>and</strong> sources<br />

code snippet BooksDemo/BooksComm<strong>and</strong>s.cs<br />

Every class that implements the IComm<strong>and</strong>Source interface can be a source of comm<strong>and</strong>s, such as<br />

Button <strong>and</strong> MenuItem. Inside the main window a Menu control is added as a child of the DockPanel.<br />

MenuItem elements are contained within the Menu control <strong>and</strong> the Comm<strong>and</strong> property is assigned to some<br />

predefined comm<strong>and</strong>s such as ApplicationComm<strong>and</strong>s.Close, <strong>and</strong> the custom comm<strong>and</strong> BooksComm<strong>and</strong>s.<br />

ShowBooks:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet BooksDemo/MainWindow.xaml<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!