Tải bản đầy đủ (.pdf) (6 trang)

Lập trình .net 4.0 và visual studio 2010 part 57 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (191.73 KB, 6 trang )

CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

377
New Controls
WPF 4.0 now contains the DataGrid, Calendar, and DatePicker controls that were previously available as
part of the WPF toolkit. Microsoft says that these controls are nearly 100 percent compatible with their
Silverlight relations. Figure 15-16 shows the new Calendar and DatePicker controls; Figure 15-17 shows
the DataGrid control in action.

Figure 15-16. Calendar and DatePicker controls

Figure 15-17. DataGrid control
The easiest way to create these controls is to drag them from the toolbox or add them manually with
the following XAML:

<Calendar></Calendar>
<DatePicker/>
<DataGrid></DataGrid>
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

378
Ribbon Control and Bag O’Tricks
Microsoft says that shortly after the release of VS2010 it will introduce a new WPF Ribbon control. The
Ribbon control could work particularly well in conjunction with the new touchscreen APIs. A CTP of the
Ribbon control is available at
Microsoft will also be making an out-of-band release that will contain the following controls under
the collection name “Bag O’Tricks” (sorry, no information is available at time of writing, although
judging by the control names you can have a pretty good guess at what they do):
• AnimatingTilePanel • NumericUpDown
• ColorPicker • Reveal
• InfoTextBox • TransitionsPresenter


• ListPager • TreeMapPanel
Windows 7 Integration
Windows 7 has some great new UI features such as jump lists, taskbar overlays, and progress indicators.
WPF 4.0 allows you to add these features to your applications. It is also worth noting that in this release
WPF dialogs now have the same feel of Vista and Windows 7 (depending what they are running on).
Jump Lists
Jump lists, which allow you to easily perform common tasks, are activated by right-clicking an
application on the task bar. Figure 15-18 shows the jump list for Windows Live Messenger.

Figure 15-18. Jump list in Windows 7
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

379
Like most things in WPF/Silverlight, jump lists can be created programmatically and declaratively.
The following code shows how to create a jump list to open Internet Explorer and Notepad that you
would define in your MainWindow.xaml:

using System.Windows.Shell;



JumpList appJumpList = new JumpList();

//Configure a JumpTask
JumpTask jumpTask1 = new JumpTask();
jumpTask1.ApplicationPath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";
jumpTask1.IconResourcePath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";

jumpTask1.Title = "IE";
jumpTask1.Description = "Open IE";


JumpTask jumpTask2 = new JumpTask();
jumpTask2.ApplicationPath = @"C:\Windows\System32\notepad.exe";
jumpTask2.IconResourcePath = @"C:\Windows\System32\notepad.exe";

jumpTask2.Title = "Notepad";
jumpTask2.Description = "Open Notepad";

appJumpList.JumpItems.Add(jumpTask1);
appJumpList.JumpItems.Add(jumpTask2);

JumpList.SetJumpList(App.Current, appJumpList);
Task Bar
Windows 7 applications can communicate progress and application status via the task bar. For example,
Figure 15-19 shows IE indicating download progress.

Figure 15-19. IE indicating download progress
WPF 4.0 Windows 7 task bar APIs give you control over the following:
• Progress bar overlay (refer to Figure 15-19).
• Icon overlay through the Overlay property (e.g., a small picture).
• Thumbnail window (a window that pops up showing a miniview of the
application's window). Note that you can pick which bit of the window is shown
using the ThumbnailClipMargin property.
Let’s take a look at how to work with the progress bar. The progress bar allows you to specify a
double value between 0 and 1 to indicate your application's progress with the ProgressValue property.
You can also indicate different types of status by specifying the ProgressState property. This has five
different settings that change the color of the bar:
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

380

• Error (red)
• Indeterminate (green)
• None (not shown)
• Normal (green)
• Paused (yellow)
You will now see how to work with this by setting a progress bar at 50% for the application:
1. Create a new WPF application called Chapter15.ProgressBar.
2. Open MainWindow.xaml.cs and add the following using statement:
using System.Windows.Shell;
3. Amend the code to the following:
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{

TaskbarItemInfo taskBarItemInfo = new TaskbarItemInfo();

taskBarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
this.TaskbarItemInfo = taskBarItemInfo;
taskBarItemInfo.ProgressValue = 0.5d;

}
4. If you now run your application, you should find the progress bar at 50%.
Multitouch Functionality
Probably one of the most interesting features in WPF 4.0 is multitouch functionality. Multitouch allows
your application to work with touch input and gestures (e.g., you can spin an image around by rotating

your hand).
Multitouch support is Windows 7 only and is enabled by setting the IsManipulationEnabled
property on an element to true and then handling the various events that the APIs expose. It's worth
noting that multitouch functionality is compatible with Surface SDK 2.0 (the world’s most expensive but
cool table).
ContentElement, UIElement, and UIElement3D elements support the following events:
• PreviewTouchDown
• TouchDown
• PreviewTouchMove
• TouchMove
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

381
• PreviewTouchUp
• TouchUp
• GotTouchCapture
• LostTouchCapture
• TouchEnter
• TouchLeave
Besides simple touch-related events WPF4.0 also supports various gestures. You can restrict the
manipulations that can be performed in the ManipulationStarted event by specifying the
ManipulationMode.
WPF4.0 supports the following gesture-related events:
• ManipulationStarted
• ManipulationDelta
• ManipulationInertiaStarting
• ManipulationCompleted
• ManipulationBoundaryFeedback
At present, hardware support for multitouch is a bit limited and expensive (and who wants grubby
fingerprints on their monitors?), but expect this to rapidly change in 2010.

Probably the best known multitouch device is the Dell Latitude XT2 and HP touchsmart. If you don’t
want to fork out for one of these devices you could give the clever work around with two mice here:
/>touch-screen/
.
MSDN has a good simple example demonstrating WPF’s touch functionality: http://
msdn.microsoft.com/en-us/library/ee649090(VS.100).aspx.
Binding Changes
Besides the very welcome new Binding window in VS2010, there are a number of other changes in the
exciting world of binding.
Run.text
Run.text is now a dependency property, which means you can now bind to it (one way) unlike previous
releases of WPF.
Dynamic Binding Support
WPF4.0 supports binding to properties implementing the IDynamicMetaObjectProvider interface such as
ExpandoObject and anything inheriting from DynamicObject ( (see Chapter 3).
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

382
Input Bindings Now Support Bindings
In previous releases of WPF, it was quite tricky to set binding to input keys using the InputBinding class.
This was because the Command property was not a dependency property and also didn’t inherit the
parent's data context. This could make certain scenarios such as implementing the MVVM pattern
difficult.
This is resolved in WPF 4.0. InputBinding, MouseBinding, and KeyBinding now inherit from Freezable
and various related properties are now made dependency properties. This should then allow you to
write input binding XAML such as the following:

<Window.InputBindings>
<KeyBinding
Modifiers="Control" Key="L"

Command="{Binding MyCustomCommand}" />
</Window.InputBindings>
Text-Rendering Improvements
In previous releases of WPF, text could sometimes appear a bit blurry. This is fixed in WPF 4.0 (possibly
driven by the need for clear text in VS2010 IDE) and you now have much finer-grained control over how
text is rendered with the new TextFormattingMode and TextRenderingMode properties.
TextOptions.TextFormattingMode
TextFormatting mode allows you to set the text metrics that WPF will use for formatting text.
TextFormatting mode has two settings:
• Ideal (as per previous versions)
• Display (ensures that every glyph’s position and width is not fractional, which is
very similar to how the GDI renders text)
The following code demonstrates setting text to use the Display setting:

<TextBlock TextOptions.TextFormattingMode="Display">
Hello I am using new Display mode formatting
</TextBlock>

Setting text to Display mode will in many cases make the text look darker and clearer. In Figure 15-
20 the first paragraph uses the old Ideal setting; the second uses the new Display setting (yes, the
difference is subtle, especially in print, but try it for yourself).

×