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). CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 383 Figure 15-20. Display mode property Ideal mode works well for situations when you have large fonts or perform transformations/zoom into the text but can look a bit blurry at small font sizes. In these cases you would probably be better off using the Display setting. TextOptions.TextRenderingMode The TextRendering setting, which allows you to control how text is anti-aliased, has four settings: • Auto (uses clear type unless disabled) • Aliased (disables anti-aliasing) • Grayscale (uses grayscale anti-aliasing) • Cleartype (uses clear type anti-aliasing) The following code shows how to apply the Grayscale rendering mode: <TextBlock TextOptions.TextRenderingMode="Grayscale"> I am rendered using Grayscale </TextBlock> Figure 15-21 shows how these settings effect the output. Figure 15-21. Demonstration of TextRendering setting CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 384 Microsoft recommends that for most scenarios Auto is the best setting to use because it takes advantage of ClearType where available. RenderOptions.ClearTypeHint In some rendering situations (such as rendering on transparent areas), ClearType functionality will be disabled and Grayscale rendering will be used. This can result in text that is not as sharp as it could be. WPF 4.0 contains a new option called ClearTypeHint to force applications to utilize ClearType. The following code illustrates how to apply this to a TextBlock: <TextBlock RenderOptions.ClearTypeHint="Enabled"> I will use cleartype </TextBlock> East Asian Bitmap font support Some non-English alphabet characters can be quite complex (think Chinese glyphs), and when rendered at smaller sizes can appear very blurry using vector transformations. WPF now uses bitmaps for smaller text size (if available) which can result in crisper text. Microsoft say this feature is supported for the following languages and fonts such as: • Japanese (MS Gothic) • Korean (Gulium) • Korean (Batang) • Traditional Chinese (MingLiu) • Simplified Chinese (SimSun) Layout Rounding When positioning elements in WPF you can sometimes end up with fractional values. For example, splitting a grid 100 pixels wide into 3 columns of equal size gives each column a nasty width of 33.333333. These fractional properties can result in images and objects with blurry edges and other horrid rendering artifacts. To see many examples, see this excellent site that visually demonstrates the affects of subpixel layouts: http://blogs.msdn.com/text/archive/2009/08/27/layout-rounding.aspx. Silverlight 2.0 introduced a new property called UseLayoutRounding that offers a solution to this issue by forcing the layout to use whole pixel values only. UseLayoutRouting is now supported in WPF. Using this feature can result in crisper images and layouts, but your layout might not be pixel perfect. This XAML demonstrates how to use this property: <Grid UseLayoutRounding="True" > </Grid> Cached Composition Arguably one of the best additions to WPF 4.0 is cached composition, that allows you to cache any part of the visual tree. Complex effects can take time to render, which results in a jerky experience for your users and uses vast amounts of CPU and memory. WPF 4.0 allows you to cache elements as a bitmap, CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 385 reducing this rendering time and resource usage with the new BitmapCache and BitmapCacheBrushes classes. The BitmapCacheBrushes class is used when you will reuse the same content multiple times. Cached composition supports dirty regions, so it is clever enough to re-render only the parts that have changed. Re-rendering can occur when WPF detects the visual tree changes or any cache-related properties are modified. Note that the maximum dimensions the bitmap cache supports are 2048 by 2048 pixels. There is an excellent demo by Lester Lobo that shows the difference cached composition can make: http://blogs.msdn.com/llobo/archive/2009/11/10/ new-wpf-features-cached-composition.aspx. CacheMode can be turned on with the following XAML (applied to a Canvas element in this example): <Canvas.CacheMode> <BitmapCache /> </Canvas.CacheMode> <Canvas x:name="myCanvas" CacheMode="BitmapCache"/> Or programmatically: myCanvas.CacheMode = new BitmapCache(); And turned off with the following code: myCanvas.CacheMode = null; Animation Easing WPF contains new effects for creating nonlinear movements using complex mathematical formulas to produce effects such as bouncy spring animations. You will look at how to utilize these in Silverlight 3.0 later in the chapter, but know that WPF 4.0 provides the following effects: • BackEase • BounceEase • CircleEase • CubicEase • Elasticease • ExponentialEase • Quadraticease • QuarticEase • Quinticease • PowerEase • SineEase CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 386 Pixel Shader 3.0 Support Previous releases of WPF supported Pixel Shaders version 2.0. WPF 4.0 now supports Pixel Shader version 3.0. Note that the hardware the application is running on must also support the Pixel Shader capabilities. To query this, use the static methods on the RenderCapability class such as RenderCapability.IsPixelShaderVersionSupported. Visual State Manager Integration Visual State Manager (VSM)) allows you to define a set of states for your controls (e.g., normal, mouse over, mouse down) and then define a different look for each of these states. VSM will automatically animate the transitions between states; for example, if you have a black button with a mouse down state that highlights it blue, the button can gradually be highlighted blue as the user hovers the mouse. In WPF 4.0, the VisualStateManager and related classes are added to the main framework. HTML-XBAP Script Interop HTML-XBAP applications can use the new BrowserInteropHelper class to interact with the hosting web page. BrowserInteropHelper provides full DOM access and can handle DOM events. Full-Trust XBAP Deployment In previous releases of WPF, it was quite difficult to create a fully trusted XBAP application. That changes with this release; XBAP applications that require full trust that are run from intranet or trusted site zones will now give users the ClickOnce elevation prompt. This allows users to easily grant the necessary privileges for your application. Client Profile It is worth mentioning the client profile (a cut-down version of the full .NET Framework) aimed at reducing application size and installation time is also used for WPF applications. For more information about the client profile, please refer to Chapter 4. Miscellaneous Changes You have barely touched the surface with all the new functionality available in WPF 4 but4.0 before you leave this area I would like to mention a number of other additions that were made: • New XAML parser • Many additions to XAML 2009 language such as support for Generics • RichTextBox now supports custom dictionaries rather than just using the OS- provided dictionary (http://blogs.msdn.com/text/archive/2009/10/02/custom- dictionaries.aspx) • Text selection can be customized for TextBox, RichTextBox, FlowDocumentPageViewer, FlowDocumentScrollViewer, FlowDocumentReader, and PasswordBox with the new Selection Brush API CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 387 • Many changes to API and refactoring of XamlSchemaContext for performance improvements • System.Xaml.dll no longer has a dependency on WindowsBase.dll • The same XAML stack is utilized by WCF, WF, and WPF • Performance optimizations in Baml2006Reader class • New class XamlXmlReader • Improved localization support • Baml2006Writer class might be available with this release, which could potentially allow the obfuscation of BAML Silverlight 3.0 Silverlight developers are in for a treat with the latest version of Silverlight which offers the ability to run your applications offline, deep linking for content and much more. NOTE This chapter assumes a basic knowledge of Silverlight and WPF. If you haven’t used Silverlight before you might want to take a look at Chapter 14 where I introduce Silverlight. Upgrading from Silverlight 2 Before you look at the new changes in Silverlight 3.0, note that upgrading can potentially break existing applications. This URL lists breaking changes: http://msdn.microsoft.com/en- us/library/cc645049(VS.95).aspx. And this URL provides guidance on how to upgrade your Silverlight 2.0 applications: http://msdn.microsoft.com/en-us/library/cc645049(VS.95).aspx. Offline Applications Probably the best feature of Silverlight 3.0 is the ability it offers to run your applications offline. Offline Silverlight applications run with the same permissions as their web counterparts, so they do not require additional permissions to install. Offline applications also work on both PC and Mac platforms, providing a very easy way to create cross-platform .NET applications. Creating an Offline Application To enable your Silverlight applications to run offline is very easy and involves making a simple change to the AppManifest file. Try it now: 1. Create a new Silverlight application called Chapter15.Offline. 2. Add some content (e.g., an image). . CHAPTER 15 WPF 4. 0 AND SILVERLIGHT 3 .0 386 Pixel Shader 3 .0 Support Previous releases of WPF supported Pixel Shaders version 2 .0. WPF 4. 0 now supports Pixel Shader version 3 .0. Note that. supports are 2 04 8 by 2 04 8 pixels. There is an excellent demo by Lester Lobo that shows the difference cached composition can make: http://blogs.msdn.com/llobo/archive/ 200 9/11/ 10/ new-wpf-features-cached-composition.aspx http://msdn.microsoft.com/en- us/library/cc 645 04 9 (VS.95).aspx. And this URL provides guidance on how to upgrade your Silverlight 2 .0 applications: http://msdn.microsoft.com/en-us/library/cc 645 04 9 (VS.95).aspx. Offline