Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 45 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
45
Dung lượng
3,23 MB
Nội dung
CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 372 Figure 15-8. Improved error handling for user controls Static Resource and Designer Fix Previously, if a static resource was declared in app.xaml, you could not use it in the designer. In VS2010, you can. Drag-and-Drop Data Binding I’m not a big fan of drag-and-drop data binding and I think it’s fair to say that most programmers will rightly scorn such functionality. However, this type of feature can be very useful for quickly putting together simple data entry forms and prototyping applications. VS2010 includes new functionality to easily create data forms and creates some pretty clean XAML. It seems likely that Microsoft might have brought in this feature to encourage some winforms developers to move over. You will now learn about this feature by creating a simple master detail form. 1. Select Data Show Data Sources. 2. Click the Add New Data Source button on the Data Sources window, and the Choose a Data Source Type window will appear (see Figure 15-9). CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 373 Figure 15-9. Choose a Data Source Type dialog 3. Select Database and then click Next. The Choose a Database Model screen will appear (see Figure 15-10). Figure 15-10. Choose a Database Model dialog 4. Select Dataset (you could also use an entity data model for this feature) and then click Next. CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 374 5. In the next screen, select the connection to the example database connection (or create it if you haven’t already) and then click Next. 6. A new screen (similar to Figure 15-11) will now appear, allowing you to select the objects you want to use. Figure 15-11. Choose your poison (sorry) database objects 7. Select the Tables node to select all the tables and change the dataset name to Chapter15Dataset before clicking Finish. 8. You now have the data source set up and if you look over to the Data Sources window (see Figure 15-12), you will see all the tables and their fields. Figure 15-12. Data Sources window after configuring the data source CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 375 9. You will now create a data grid to show all the films. You don’t want the field FilmID to appear, so click the FilmID beneath the Film table node. A drop-down menu will then appear, allowing you to select different types of UI to be generated for the field. In this case, you don’t want it to appear, so select None. 10. Now drag the Film table node onto the designer surface to create a DataGrid of films (see Figure 15-13). If you run your project now, you will find a fully working DataGrid. However, you’re not done yet; VS2010 also supports the ability to easily create master detail forms. Figure 15-13. Data grid linked to Data source 11. You will now create a details panel to show more details of the film. Click FilmID (beneath the Film node) and change the type to Textbox. 12. Now click the Film node itself and on the drop-down menu change it to Details before dragging it to the right of the data grid (you might want to reposition the grid). 13. You also want to display all the film showings, so click the FilmShowing node and change the drop-down menu to List, and then drag it to the page beneath the grid. 14. Now press F5 to run your application and you will have a fully functional master details view similar to Figure 15-14. CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 376 Figure 15-14. The final product of the drag-and-drop binding Improved XAML Intellisense XAML Intellisense has been tweaked. Now when you enter the curly bracket for a binding expression, Intellisense will add the other curly bracket for you and pop up a dialog with available options (see Figure 15-15). It seems a shame the team didn’t go further with this feature and show you different objects you could bind to. Figure 15-15. Improved XAML intellisense 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 http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117. 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: http://blog.wpfwonderland.com/2009/06/29/developing-win-7-multi-touch-apps-without-multi- 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). [...]... SaveFileDialog SaveDialog = new SaveFileDialog(); if (SaveDialog.ShowDialog() == true) { System.IO.Stream fs = null; try { fs = SaveDialog.OpenFile(); byte[] info = (new System.Text.UTF8Encoding(true)).GetBytes("Test text to write to file"); fs.Write (info, 0, info. Length); } finally { fs.Close(); } } } 395 CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 Filtering Files in SaveDialog Files shown in the SaveDialog window... method that returns true if the user is connected: System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() CAUTION If the user has chosen to work in offline mode, this method will still return true You also have the ability to monitor network address changes through the NetworkAddressChanged event: System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged() Autoupdate When offline... applications 391 CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 Figure 15-26 Default Navigation Application Local Connections Some page designs utilize two separate Silverlight controls and need a way to pass information between them Previously, this could only be accomplished using the HTML DOM methods Silverlight 3.0 makes this much easier with the local connection API Let’s create a simple example and send... 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... by utilizing a language called High Level Shader Language (HLSL).) 401 CHAPTER 15 WPF 4.0 AND SILVERLIGHT 3.0 Renew Schulte created an excellent filter that simulates an old movie tape effect For more information on how René created this effect and an example of this applied to a movie please refer to http://kodierer.blogspot.com/2009/08/ye-olde-pixels-silverlight-3-old-movie.html Media One of Silverlight's... capabilities (indeed version 1.0 wasn’t good for much else) Silverlight 3.0 now contains support for a number of new formats and smooth streaming when used in conjunction with IIS Media Services (http:/ /www. iis.net/media) New Formats Silverlight 3.0 supports the following additional formats: • HD (720p+) • MPEG-4–based H.264/AAC Audio ( known as HD TV) • HD playback to full screen by utilizing GPU Silverlight... run other desktop programs (e.g., Microsoft Office suite) • New interface for requesting more privileges for applications • Localization enhancements (right-to-left text and complex scripts) For more information, please refer to http://silverlight.net/getstarted/silverlight-4-beta/ Silverlight in the Real World Some developers consider Silverlight little more than a toy at present and argue that it... line of business application? I talked to Rusty and Andy at SharpCloud about their experiences of developing a Silverlight application Rusty Johnson and Andy Britcliffe, SharpCloud SharpCloud (http:/ /www. sharpcloud.com) is currently developing a “project risk assessment application.” The application carries out complex calculations to determine the optimum order for carrying out projects The application . TaskbarItemInfo taskBarItemInfo = new TaskbarItemInfo(); taskBarItemInfo.ProgressState = TaskbarItemProgressState.Normal; this.TaskbarItemInfo = taskBarItemInfo; taskBarItemInfo.ProgressValue. to the Internet. This can be accomplished through the GetIsNetworkAvailable method that returns true if the user is connected: System .Net. NetworkInformation.NetworkInterface.GetIsNetworkAvailable(). true. You also have the ability to monitor network address changes through the NetworkAddressChanged event: System .Net. NetworkInformation.NetworkChange.NetworkAddressChanged() Autoupdate When