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:
For example:
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:
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):
The program source code is as follows:
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.