《C#高级编程》读书笔记(二十):核心XAML
Posted 逍遥king
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《C#高级编程》读书笔记(二十):核心XAML相关的知识,希望对你有一定的参考价值。
1,关于XAML
编写 WPF 应用程序、使用 WF、创建 XPS 文档 、编写 Silverlight 或者 Windows 8 应用程序,就还需要 XAML。XAML(eXtensible Application Markup Lauguage,可扩展应用程序标记语言)是一种声明性的 XML 语法。
XAML 代码使用文本XML来声明。XAML 代码可以使用设计器创建,也可以手动编写。
2,将元素映射到.NET对象上
创建C#控制台项目,引用程序集 PresentationFramework、PresentationCore、WindowBase 和 System.Xaml。
using System; using System.Windows; using System.Windows.Controls; namespace ConsoleXAML { class Program { [STAThread] static void Main(string[] args) { var b = new Button() { Content = "Click Me!" }; var w = new Window() { Title = "Code Demo", Content = b }; var app = new Application(); app.Run(w); } } }
3,使用自定义.NET 类
namespace XAMLIntro { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { 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="350" Width="525"> <Grid> <Button Content="Click Me!" Height="20" Width="100" 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,事件的冒泡和隧道
元素可以包含在其他元素中。内层元素的事件可以传递到外部,这就冒泡事件。外部向内部移动,叫隧道事件。WPF 支持事件的冒泡和隧道。
<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="350" 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>
using System.Collections.ObjectModel; using System.Windows; namespace ButtleDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private ObservableCollection<string> messages = new ObservableCollection<string>(); public MainWindow() { InitializeComponent(); this.DataContext = messages; } private void AddMessage(string message, object sender, RoutedEventArgs e) { messages.Add( $"{message}," + $"sender:{(sender as FrameworkElement).Name};" + $"source:{(e.Source as FrameworkElement).Name};" + $"original source:{(e.OriginalSource as FrameworkElement).Name}"); } private void OnOuterButtonClick(object sender, RoutedEventArgs e) { AddMessage("最外层单击", sender, e); } private void OnButton2(object sender, RoutedEventArgs e) { AddMessage("Button2单击",sender,e); e.Source = sender; } private void OnInner1(object sender, RoutedEventArgs e) { AddMessage("Inner 1 Click!",sender,e); } private void OnInner2(object sender, RoutedEventArgs e) { AddMessage("Inner 2 Click!", sender, e); e.Handled = true; } } }
以上是关于《C#高级编程》读书笔记(二十):核心XAML的主要内容,如果未能解决你的问题,请参考以下文章