Author: Dino Esposito translation: winsome zhong
Third, XAML Language XAML consists of four types of elements - panels, interactive controls, document objects (Documents), and grapes. In addition, the Border element is also important, although it does not belong to the four major elements. In XAML, Panel is responsible for managing page layout and is a container of other elements, controlling their size, location, and content of the content. Longhorn built 6 PANEL classes - Canvas, DockPanel, FlowPanel, Gridpanel, Table, and TextPanel. But developers can create their own Panel to customize their drawing behavior. Canvas is the only support coordinate in the panel, which is drawn from a fixed location, with a specific size. If the two control elements overlap, the one of the draws will overwrite the previous one. Note that the coordinates are relative to Canvas, (0, 0) represents the top left corner of Canvas. Below is an example of a Canvas object: , DockPanel is not like this, DockPanel All control elements are vertical or horizontally Dock, and the coordinates are not required on the DockPanel, and the control position is relative to the previous control. DockPanel is an ideal choice for creating toolbars.
For example: Open save Print Figure 6, the first button is arranged in the left side of Panel, and other buttons are arranged in turn. Figure 6 Button Layout
FlowPanel is another way, it makes the above control along one direction, and when the control exceeds the width or height of Panel, you can set it to let it fold to the next line or take an exceeded part of the part Do not show. The following example demonstrates how FlowPanel is folded. There are 4 squares on the FlowPanel (actually border), and their width is greater than the Width of FlowPanel. Therefore, all squares cannot be displayed in one line, and the last square is displayed in the second line. FlowPanel default arrangement is from left to right, from top to bottom: GridPanel is a lightweight layout manager that lays the above controls like a grid, thus constructing a simple form. GridPanel allows you to put any controls on rows or columns, combine into a control matrix. But the function of GridPanel is still relatively simple, to construct a more advanced table, you can use Table Panel. Table Panel is used to display some complex table data, which supports some advanced table features such as Header, Footer, Column, Row Group, and so on. Finally, TextPanel is used to optimize text display, which supports some complicated text formats. However, for basic text display, use Text may be a better choice, Text is used to display text without format. All controls with users interact with users are in the MsavaLon.Windows.controls namespace. For example, Button, ComboBox, ListBox, Checkbox, ContextMenu, RadiobuttonList, and PageViewer et al. The base class Control of the interactive control, provides the property and method of control public. A relatively particularly PageViewer, this control is used to view the online document and encapsulate paging and navigation. The following script generates a PageViewer, which is displayed in the text pagination in the file specified in the Source property.
See a pageviewer control in action below .
OK Running results as shown in Figure 7 Page Viewer
Interestingly, Page View cannot show pure TXT, RTF or HTML, at least this is the case. The file specified in the source property of PageViewer must be an XAML file that is displayed in the file. The ASCII text that is displayed in support paging (Avalon is a new display subsystem of the Longhorn), such as TextPanel. The following code is stored as sample.xaml, as a file displayed in the previous example (Note that XMLns is required): text to show goes here Namespace MSAVALON.WINDOWS.Documents are responsible for documentation. These sets of collections look like the HTML tag expansion, including Block, Column, Heading, Footer, ColumNGroup, RowGroup, HyperLink, and List. Longhorn uses Windows Vector Graphics (WVG) to draw graphics content, WVG has a lot of advantages over GDI and GDI , which is an XML-based graphics system, which is easy to use and reuse, for SVG enthusiasts It is a good news. WVG predefined graphics have Ellipse, Line, Path, Polygon, Polyline, and Rectangle. They are all subclasses of Shape. Shape can be tilted, rotated and transparent. In addition to using solid color as a background, you can specify the background of progressive colored colors. The following example uses horizontal progressive color to fill an ellipse, starting from red to blue: 4, build an example using the XAML language, you can quickly and efficiently design the user interface. I dare to guarantee that when LONGHORN is released, many of the XAML development tools that are fully supported and what they have received will be updated or developed. The currently released preview version of the WHIDBey has included a part of this extension. Let us build a sample program. You will see this and write a Windows Form Application is not too big, and you will also be surprised to discover the skill you have mastered. Net skills can be applied to Longhorn. The user interface of this program is shown in Figure 8, including a text basket and a context menu. The text basket is defined with XAML, and the context menu is dynamically created. Figure 8 context menu
The program source code is as follows: Right-Click to See a context menu // c # Source File for the application
Namespace msdnlhsample {public partial class myapp: Application {void AppStartingup (Object sender, startingupcanceleventargs e) {window mainwindow = new window1 (); mainwindow.show ();}}}
// c # Source File for the window
using System; using MSAvalon.Windows; using MSAvalon.Windows.Media; using MSAvalon.Windows.Controls; using MSAvalon.Windows.Documents; using MSAvalon.Windows.Navigation; using MSAvalon.Windows.Shapes; using MSAvalon.Windows.Data;
Namespace MSDNLHSAMPLE {
// INPUT Is The TextBox Decland in the Xaml
Public Partial Class Window1: Window {Private Void OnContextMenu (Object Sender, ContextMenueventargs E) {creteMenu ();}
private void CreateMenu () {ContextMenu cm = new ContextMenu (); cm.FontFamily = "verdana"; cm.Background = Brushes.LightYellow; MenuItem mi0 = new MenuItem (); mi0.Header = "Lower case"; mi0.FontSize = New FONTSIZE (16); Mi0.Click = New CLICKEVENTHANDLER (LowerCase);
CM.Items.Add (MI0);
Menuitem mi1 = new me1.fontsize = new fontsize (16); mi1.header = "Upper case"; mi1.click = new clickeventhandler (Uppercase); cm.items.add (MI1);
Menuitem mi2 = new meut.Header = "SELECT All"; mi2.fontsize = new fontsize (16); mi2.click = new CLICKEVENTALL (SELECTALL); cm.items.add (mi2);
Input.contextMenu = cm;}
public void LowerCase (Object sender, ClickEventArgs args) {MenuItem mi = (MenuItem) args.Source; ContextMenu menu = (ContextMenu) mi.Parent; TextBox thisTextBox = (TextBox) menu.PlacementTarget;
THisTextBox.Text = thistextbox.text.tolower ();
public void UpperCase (Object sender, ClickEventArgs args) {MenuItem mi = (MenuItem) args.Source; ContextMenu menu = (ContextMenu) mi.Parent; TextBox thisTextBox = (TextBox) menu.PlacementTarget;
THisTextBox.text = thistextbox.text.toupper ();
public void SelectAll (Object sender, ClickEventArgs args) {MenuItem mi = (MenuItem) args.Source; ContextMenu menu = (ContextMenu) mi.Parent; TextBox thisTextBox = (TextBox) menu.PlacementTarget; thisTextBox.SelectAll ();}
}} All applications in Longhorn are all an instance of all applications in Longhorn. This object is the core of the longhorn application model. But Application object only provides some basic functions, generally only for some low-level, no programs that require navigation functions. In fact, most Longhorn applications use A subclass of Application --NavigationApplication, which adds support for navigation. NavigationApplication supports a large number of properties, methods, and events, allowing you to combine a lot of XAML Page into an application. To make such a comparison may be more clearly: Based on the APPLICATION class LONGHORN application similar to the Win32-based dialog-based program. Based on NavigationApplication, navigable Longhorn program is equivalent to a complete Win32 program that includes numerous forms to complete the functionality of the program. Below is an example of how to navigate in Page: myapp = navigationApplication.current; win = (navigation.navigationWindow) myapp.windows [0]; ...... private void button_back (Object sender, Clickeventargs E) {// If Possible, Go to the Previous Window if (Win.cangoback ()) win.goback ();} You can get references to Application objects through the static attribute of Application (or NavigationApplication). The previous example also demonstrates how to get references to the first form from the stack. In the Button_BACK event, first check if the first form object exists, if there is a jump over. The following example demonstrates how the behavior and covering it by inheriting NavigationApplication OnStartingUp ways to customize the program starts: public class MyApp: NavigationApplication {NavigationWindow win; ...... protected override void OnStartingUp (StartingUpCancelEventArgs e) {win = new NavigationWindow (); // add elements to the window ... navwin.show ();} ......}
Let's go back to the example of Figure 8. Based on our discussion, this example can use Application as the base class, or use NavigationApplication, because this is just a single-store application, I use Application, the program covers AppStartingup method, and defines Several event processors. AppStartingup is an ideal location for system initialization. In AppStartingUP, we created this example of the main form, the text basket in the form is described in XAML, the text basket is bound to the ContextMenuevent event, triggered the event when the user triggers the text, creating a context menu Object (ContextMenu). The context menu uses a graphical designer design, by selecting background colors, foreground, borders, fonts, etc., you can easily customize menu (and other controls). This requires many programming work in Wind32 programming, and in .NET Framework, managed code encapsulates most of the properties, and users can control less attributes. Menu items use the MenuiteM class to create, and the way they bind them to the event processor is almost the same as .NET: MIA = New Menuitem [3]; for (int i = 0; i <3; i ) { MIA [I] = new menuItem (); cm.items.add (MIA [i]); MIA [i] .foreground = brushes.black;} mia [0] .Header = "Lower Text"; Mia [1] . HEADER = "Upper case"; Mia [2] .Header = "select all"; mia [0] .click = new clickeventhandler (LowerCase); MIA [1] .Click = new CLICKEVENTHANDLER (Uppercase); MIA [ 2] .Click = new CLICKEVENTHANDLER (SELECTAL); When a menu item is clicked, the corresponding event processor is executed, the first parameter of the event processor is assigned a reference to a menu item: public void LowerCase Object sender, ClickEventArgs args) {MenuItem mi = (MenuItem) args.Source; ContextMenu menu = (ContextMenu) mi.Parent; TextBox thisTextBox = (TextBox) menu.PlacementTarget; thisTextBox.Text = thisTextBox.Text.ToLower ();} In order to get the TextBox object, you must trace from the structure. First, get the MenuItem object, then the ContextMenu object, finally get the Owner: TextBox of ContextMenu, then modify the contents of the TextBox object, it is easy.