"C # Advanced Programming" Reading notes (20): Core XAML

Source: Internet
Author: User

1, about XAML

XAML is also required to write WPF applications, use WF, create XPS documents, write Silverlight, or Windows 8 applications. XAML (extensible Application Markup Lauguage, Extensible Application Markup Language) is a declarative XML syntax.

XAML code is declared using text XML. XAML code can be created using the designer, or it can be written manually.

2, the element is mapped to. NET Object

Create a C # console project referencing Assemblies PresentationFramework, PresentationCore, Windowbase, and System.Xaml.

usingSystem;usingSystem.Windows;usingSystem.Windows.Controls;namespaceconsolexaml{classProgram {[STAThread]Static voidMain (string[] args) {            varb =NewButton () {Content="Click me!"            }; varW =NewWindow () {Title="Code Demo", Content=B}; varApp =Newapplication (); App.        Run (w); }    }}
3, using custom. NET Classes
namespacexamlintro{ Public classPerson { Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public Override stringToString () {return$"{FirstName} {LastName}"; }    }}
<window x:class="Xamlintro.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:xamlintro" mc:ignorable="D"Title="MainWindow"height=" -"Width="525"> <Grid> <button content="Click me!"height=" -"Width=" -"Background="Lightgoldenrodyellow"click="Button_Click"></Button> <listbox margin="193,35,194,204"><local:person firstname="Stephanie" lastname="Nagel" ></local:Person> <local:person firstname="Matthias" lastname= " Nagel "></local:Person></ListBox> <button margin="208,198,201,93"Horizontalalignment="Center"Verticalalignment="Center">Click Test! </Button> </Grid> <!--<x:array type="Local:person"> <local:person firstname="Stephanie"Lastname="Nagel"></local:Person> <local:person firstname="Matthias"Lastname="Nagel"></local:Person> </x:Array>--></Window>
4, bubbling and tunneling of events

Elements can be included in other elements. Events of the inner element can be passed to the outside, which is the bubbling event. The outside moves inward, called the tunnel event. WPF supports bubbling and tunneling of events.

<window x:class="Buttledemo.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="Clr-namespace:buttledemo"mc:ignorable="D"Title="MainWindow"height=" -"Width="525"> <stackpanel x:name="StackPanel1"button.click="Onouterbuttonclick"> <button x:name="Button1"Content="Button 1"margin="5,15,5,5"></Button> <button x:name="Button2"margin="5"click="OnButton2"> <listbox x:name="ListBox1"> <button x:name="InnerButton1"Content="Inner Button 1"margin="4"padding="4"click="OnInner1"></Button> <button x:name="InnerButton2"Content="Inner Button 2"margin="4"padding="4"click="OnInner2"></Button> </ListBox> </Button> <listbox itemssource="{Binding}"></ListBox> </StackPanel></Window>

usingSystem.Collections.ObjectModel;usingSystem.Windows;namespacebuttledemo{/// <summary>    ///the interactive logic of MainWindow.xaml/// </summary>     Public Partial classMainwindow:window {Privateobservablecollection<string> messages =Newobservablecollection<string>();  PublicMainWindow () {InitializeComponent ();  This. DataContext =messages; }        Private voidAddMessage (stringMessageObjectsender, RoutedEventArgs e) {Messages. ADD ($"{message},"+                $"sender:{(sender as FrameworkElement). Name};"+                $"source:{(E.source as FrameworkElement). Name};"+                $"Original source:{(E.originalsource as FrameworkElement). Name}"); }        Private voidOnouterbuttonclick (Objectsender, RoutedEventArgs e) {AddMessage ("Outermost Click", sender, E); }        Private voidOnButton2 (Objectsender, RoutedEventArgs e) {AddMessage ("Button2 Click", sender,e); E.source=Sender; }        Private voidOnInner1 (Objectsender, RoutedEventArgs e) {AddMessage ("Inner 1 click!", sender,e); }        Private voidOnInner2 (Objectsender, RoutedEventArgs e) {AddMessage ("Inner 2 click!", sender, E); E.handled=true; }    }}

"C # Advanced Programming" Reading notes (20): Core XAML

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.