Silverlight Tips of the Day 系列翻译与领悟#6
2008-12-10 14:49
316 查看
SilverlightTipoftheDay#6:MonitoringforKeyboardandMouseEvents.
Silverlight2提供了鼠标和键盘事件的完整支持。你不仅可以监听整个应用程序的事件还可以在独立的控件级别监听它们。下面是一个在应用程序级别监听所有键盘和鼠标事件的例子:
Page.xaml.cs:
namespaceSilverlightApplication
{
publicpartialclassPage:UserControl
{
publicPage()
{
InitializeComponent();
this.KeyDown+=newKeyEventHandler(Page_KeyDown);
this.KeyUp+=newKeyEventHandler(Page_KeyUp);
this.MouseEnter+=newMouseEventHandler(Page_MouseEnter);
this.MouseLeave+=newMouseEventHandler(Page_MouseLeave);
this.MouseLeftButtonDown+=newMouseButtonEventHandler(Page_MouseLeftButtonDown);
this.MouseLeftButtonUp+=newMouseButtonEventHandler(Page_MouseLeftButtonUp);
this.MouseMove+=newMouseEventHandler(Page_MouseMove);
}
voidPage_KeyDown(objectsender,KeyEventArgse)
{
}
voidPage_KeyUp(objectsender,KeyEventArgse)
{
}
voidPage_MouseEnter(objectsender,MouseEventArgse)
{
}
voidPage_MouseLeave(objectsender,MouseEventArgse)
{
}
voidPage_MouseLeftButtonDown(objectsender,MouseButtonEventArgse)
{
}
voidPage_MouseLeftButtonUp(objectsender,MouseButtonEventArgse)
{
}
voidPage_MouseMove(objectsender,MouseEventArgse)
{
}
}
}
要在控件级别上监听事件,你可以直接在XAML的控件上添加事件处理。在下面的例子中,我们为<Textblock>控件添加了鼠标左键按下和弹起的事件。
Page.xaml:
<UserControlx:Class="SilverlightApplication6.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400"Height="300">
<Gridx:Name="LayoutRoot"Background="White">
<TextBlockx:Name="FPS"MouseLeftButtonDown="FPS_MouseLeftButtonDown"
MouseLeftButtonUp="FPS_MouseLeftButtonUp">Hellothere</TextBlock>
</Grid>
</UserControl>
Page.xaml.cs:
namespaceSilverlightApplication
{
publicpartialclassPage:UserControl
{
publicPage()
{
InitializeComponent();
}
privatevoidFPS_MouseLeftButtonDown(objectsender,MouseButtonEventArgse)
{
FPS.Text="Mousebuttondown";
}
privatevoidFPS_MouseLeftButtonUp(objectsender,MouseButtonEventArgse)
{
FPS.Text="Mousebuttonup";
}
}
}
相关文章推荐
- Silverlight Tips of the Day 系列翻译与领悟#5
- Silverlight Tips of the Day 系列翻译与领悟#7
- Silverlight Tips of the Day 系列翻译与领悟#1
- Silverlight Tips of the Day 系列翻译与领悟#3
- Silverlight Tips of the Day 系列翻译与领悟#8
- Silverlight Tips of the Day 系列翻译与领悟#9
- Silverlight Tips of the Day 系列翻译与领悟 目录
- Silverlight Tips of the Day 系列翻译与领悟#10
- Silverlight Tips of the Day 系列翻译与领悟#0
- Silverlight Tips of the Day 系列翻译与领悟#2
- Silverlight Tips of the Day 系列翻译与领悟#4
- (转)Silverlight Tips of the Day - Summary Outline
- Android Studio Tips Of the Day – Roundup
- Android Studio Tips Of the Day - Roundup #1
- Android Studio Tips Of the Day - Roundup #2
- Sandbox 2 Tips of the Day
- Android Studio Tips Of the Day - Roundup #3
- Android Studio Tips Of the Day – Roundup
- Android Studio Tips Of the Day
- vimtips.txt 可以直接使用,无需任何修改 (TipofTheDay vim)