XAML中事件的一般概念类似于其他流行编程语言(如.NET和C ++)中的事件.在XAML中,所有控件都会公开一些事件,以便可以为特定目的订阅它们.
每当发生事件时,应用程序都会收到通知,程序可以对它们做出反应,例如,关闭按钮用于关闭对话框.
根据应用程序的要求,可以根据应用程序的不同行为订阅许多类型的事件,但最常见的是使用的事件是那些与鼠标和键盘相关的事件,例如,
点击
MouseDown
MouseEnter
MouseLeave
MouseUp
KeyDown
KeyUp
在本章中,我们将使用一些基本和最常用的事件来了解事件的方式根据用户在特定事件发生时想要做什么,特定控件的链接可以链接到将要实现行为的代码.
让我们来看看我们来看一个按钮点击事件的简单示例.下面给出了Button控件的XAML实现,它是使用一些属性和Click事件(Click ="OnClick")创建和初始化的.
每当点击此按钮时,它将触发 OnClick 事件,您可以添加任何类型的行为作为对此的响应点击.让我们看一下OnClick事件实现,它将在单击此按钮时显示一条消息.
using System; using System.Windows; using System.Windows.Controls; namespace XAMLEventHandling { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnClick(object sender, RoutedEventArgs e) { MessageBox.Show("Button is clicked!"); } } }
当你编译并执行上面的代码时,它会产生以下输出 :
当您点击按钮时,点击( OnClick)事件将被触发并显示以下消息.
现在让我们看看处理多个事件的一个复杂的例子.
示例
以下示例包含带有ContextMenu的文本框,用于处理文本框中的文本.
以下XAML代码创建一个TextBox,一个ContextMenu和一个MenuItems,其中包含一些属性和事件,如Checked,Unchecked和Click./p>
Hi, this is XAML tutorial.
以下是C#中针对选中,取消选中或点击菜单项时将触发的不同事件的实现.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace XAMLContextMenu { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Bold_Checked(object sender, RoutedEventArgs e) { textBox1.FontWeight = FontWeights.Bold; } private void Bold_Unchecked(object sender, RoutedEventArgs e) { textBox1.FontWeight = FontWeights.Normal; } private void Italic_Checked(object sender, RoutedEventArgs e) { textBox1.FontStyle = FontStyles.Italic; } private void Italic_Unchecked(object sender, RoutedEventArgs e) { textBox1.FontStyle = FontStyles.Normal; } private void IncreaseFont_Click(object sender, RoutedEventArgs e) { if (textBox1.FontSize < 18) { textBox1.FontSize += 2; } } private void DecreaseFont_Click(object sender, RoutedEventArgs e) { if (textBox1.FontSize > 10) { textBox1.FontSize -= 2; } } }}
当你编译并执行上面的代码时,它会产生以下输出 :
我们建议您执行上面的示例代码和一些其他事件的实验.
事件
Sr.No. | 控件&说明 |
---|---|
1 | Checked 选中ToggleButton时触发. (继承自ToggleButton) |
2 | Click 单击按钮控件时发生. (继承自ButtonBase) |
3 | ContextMenuClosing 在元素的任何上下文菜单关闭之前发生. (继承自FrameworkElement.) |
4 | ContextMenuOpening 在元素的任何上下文菜单打开时发生. (继承自FrameworkElement.) |
5 | DataContextChanged 在FrameworkElement.DataContext属性的值更改时发生. (继承自FrameworkElement) |
6 | DragEnter 当输入系统使用此元素作为目标报告基础拖动事件时发生. (继承自UIElement). |
7 | DragLeave 当输入系统报告以此元素为原点的基础拖动事件时发生. (继承自UIElement) |
8 | DragOver 当输入系统使用此元素作为潜在放置目标报告基础拖动事件时发生. (继承自UIElement) |
9 | DragStarting 启动拖动操作时发生. (继承自UIElement) |
10 | DropCompleted 拖放操作结束时发生. (继承自UIElement) |
11 | DropDownClosed 当ComboBox的下拉部分关闭时发生. |
12 | DropDownOpened 当ComboBox的下拉部分打开时发生. |
13 | GotFocus 当UIElement获得焦点时发生. (继承自UIElement) |
14 | Holding 在此元素的命中测试区域上发生另外未处理的保持交互时发生. (继承自UIElement) |
15 | Intermediate 当ToggleButton的状态切换到不确定状态时触发. (继承自ToggleButton) |
16 | IsEnabledChanged 当IsEnabled属性更改时发生. (继承自Control) |
17 | KeyDown 在UIElement具有焦点时按下键盘键时发生. (继承自UIElement) |
18 | KeyUp 在UIElement具有焦点时释放键盘键时发生. (继承自UIElement) |
19 | LostFocus 当UIElement失去焦点时发生. (继承自UIElement) |
20 | ManipulationCompleted 在UIElement上的操作完成时发生. (继承自UIElement) |
21 | ManipulationDelta 在操作期间输入设备更改位置时发生. (继承自UIElement) |
22 | ManipulationInertiaStarting 在操作和惯性开始期间输入设备与UIElement对象失去联系时发生. (继承自UIElement) |
23 | ManipulationStarted 当输入设备开始对UIElement进行操作时发生. (继承自UIElement) |
24 | ManipulationStarting 首次创建操纵处理器时发生. (继承自UIElement) |
25 | SelectionChanged 文本选择发生变化时发生. |
26 | SizeChanged 当ActualHeight或ActualWidth属性更改FrameworkElement上的值时发生. (继承自FrameworkElement) |
27 | Unchecked 取消选中ToggleButton时发生. (继承自ToggleButton) |
28 | ValueChanged 范围值更改时发生. (继承自RangeBase) |