Silverlight开发中的疑难杂症-如何为Silverlight添加双击事件
2010-03-12 20:14
507 查看
在日常的开发中鼠标的事件是我们使用的最多的事件,但是在Silverlight中,只支持以下六种有限的鼠标事件:MouseEnter、MouseLeave、MouseLeftButtonDown、MouseLeftButtonUp、MouseMove、MouseWheel。这给我们的开发造成了不小的麻烦,还好Silverlight支持强大的附加属性机制,这里就指导大家如何通过附加属性来给Silverlight添加鼠标的双击事件。
附加属性是Silverlight也是WPF中最具创新也是最强大的特性之一,它能够让你在不拥有控件的源码的情况下对其功能进行扩展。关于附加属性的强大我就不在这里叙述了,有感兴趣的朋友可以去查看WPF控件开发揭秘一书,里面有一张专门争对附加属性进行了介绍,网络上和园子里也早就有许多关于附加属性的文章。
这里直接进入主题,首先,我们需要对外提供两种双击事件的处理机制,一种是常用的事件机制,使用自带的MouseButtonEventHandler;另一种是现在比较流行的Command机制,使用ICommand接口。两个附加属性的定义如下:
MouseEventHelper
/// <summary>
/// Silverlight鼠标事件帮助类
/// </summary>
public class MouseEventHelper
{
#region Dependency Properties
#region 模拟鼠标双击事件
#region MouseDoubleClick
public static MouseButtonEventHandler GetMouseDoubleClick(DependencyObject obj)
{
return (MouseButtonEventHandler)obj.GetValue(MouseDoubleClickProperty);
}
public static void SetMouseDoubleClick(DependencyObject obj, MouseButtonEventHandler value)
{
obj.SetValue(MouseDoubleClickProperty, value);
}
public static readonly DependencyProperty MouseDoubleClickProperty = DependencyProperty.RegisterAttached(
"MouseDoubleClick",
typeof(MouseButtonEventHandler),
typeof(MouseEventHelper),
new PropertyMetadata(new PropertyChangedCallback(OnMouseDoubleClickChanged)));
#endregion
#region MouseDoubleClickCommand
public static ICommand GetMouseDoubleClickCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(MouseDoubleClickCommandProperty);
}
public static void SetMouseDoubleClickCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(MouseDoubleClickCommandProperty, value);
}
public static readonly DependencyProperty MouseDoubleClickCommandProperty =
DependencyProperty.RegisterAttached("MouseDoubleClickCommand", typeof(ICommand), typeof(MouseEventHelper), new PropertyMetadata(OnMouseDoubleClickChanged));
#endregion
private static DateTime? _lastClickTime = null;
private const int MaxClickInterval = 200;//ms
private static void OnMouseDoubleClickChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UIElement element = obj as UIElement;
if (element != null)
{
element.MouseLeftButtonDown += new MouseButtonEventHandler(element_MouseLeftButtonDown);
}
}
/// <summary>
/// 通过检测两次鼠标单机的间隔来模拟鼠标双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//进入双击事件
if (_lastClickTime.HasValue && DateTime.Now.Subtract(_lastClickTime.Value).TotalMilliseconds <= MaxClickInterval)
{
//触发事件
MouseButtonEventHandler handler = (sender as UIElement).GetValue(MouseDoubleClickProperty) as MouseButtonEventHandler;
if (handler != null)
{
handler(sender, e);
}
ICommand command = (sender as UIElement).GetValue(MouseDoubleClickCommandProperty) as ICommand;
if (command != null)
{
if (command.CanExecute(sender))
{
command.Execute(sender);
}
}
//重新计时
_lastClickTime = null;
}
else
{
_lastClickTime = DateTime.Now;
}
}
#endregion
#endregion
}
是不是觉得很简单?按照上面的流程大家就可以尽情的扩展Silverlight,帮她加上各种各样的功能了!~
附加属性是Silverlight也是WPF中最具创新也是最强大的特性之一,它能够让你在不拥有控件的源码的情况下对其功能进行扩展。关于附加属性的强大我就不在这里叙述了,有感兴趣的朋友可以去查看WPF控件开发揭秘一书,里面有一张专门争对附加属性进行了介绍,网络上和园子里也早就有许多关于附加属性的文章。
这里直接进入主题,首先,我们需要对外提供两种双击事件的处理机制,一种是常用的事件机制,使用自带的MouseButtonEventHandler;另一种是现在比较流行的Command机制,使用ICommand接口。两个附加属性的定义如下:
MouseEventHelper
/// <summary>
/// Silverlight鼠标事件帮助类
/// </summary>
public class MouseEventHelper
{
#region Dependency Properties
#region 模拟鼠标双击事件
#region MouseDoubleClick
public static MouseButtonEventHandler GetMouseDoubleClick(DependencyObject obj)
{
return (MouseButtonEventHandler)obj.GetValue(MouseDoubleClickProperty);
}
public static void SetMouseDoubleClick(DependencyObject obj, MouseButtonEventHandler value)
{
obj.SetValue(MouseDoubleClickProperty, value);
}
public static readonly DependencyProperty MouseDoubleClickProperty = DependencyProperty.RegisterAttached(
"MouseDoubleClick",
typeof(MouseButtonEventHandler),
typeof(MouseEventHelper),
new PropertyMetadata(new PropertyChangedCallback(OnMouseDoubleClickChanged)));
#endregion
#region MouseDoubleClickCommand
public static ICommand GetMouseDoubleClickCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(MouseDoubleClickCommandProperty);
}
public static void SetMouseDoubleClickCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(MouseDoubleClickCommandProperty, value);
}
public static readonly DependencyProperty MouseDoubleClickCommandProperty =
DependencyProperty.RegisterAttached("MouseDoubleClickCommand", typeof(ICommand), typeof(MouseEventHelper), new PropertyMetadata(OnMouseDoubleClickChanged));
#endregion
private static DateTime? _lastClickTime = null;
private const int MaxClickInterval = 200;//ms
private static void OnMouseDoubleClickChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UIElement element = obj as UIElement;
if (element != null)
{
element.MouseLeftButtonDown += new MouseButtonEventHandler(element_MouseLeftButtonDown);
}
}
/// <summary>
/// 通过检测两次鼠标单机的间隔来模拟鼠标双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//进入双击事件
if (_lastClickTime.HasValue && DateTime.Now.Subtract(_lastClickTime.Value).TotalMilliseconds <= MaxClickInterval)
{
//触发事件
MouseButtonEventHandler handler = (sender as UIElement).GetValue(MouseDoubleClickProperty) as MouseButtonEventHandler;
if (handler != null)
{
handler(sender, e);
}
ICommand command = (sender as UIElement).GetValue(MouseDoubleClickCommandProperty) as ICommand;
if (command != null)
{
if (command.CanExecute(sender))
{
command.Execute(sender);
}
}
//重新计时
_lastClickTime = null;
}
else
{
_lastClickTime = DateTime.Now;
}
}
#endregion
#endregion
}
是不是觉得很简单?按照上面的流程大家就可以尽情的扩展Silverlight,帮她加上各种各样的功能了!~
相关文章推荐
- Silverlight开发中的疑难杂症-如何为Silverlight添加默认按钮
- 开发手记之-在Winform中为ListBox的添加选项值,并增加双击事件
- IOS开发中如何给UIImageView添加点击事件
- Silverlight开发中的疑难杂症-如何通过代码附加Behavior
- 如何自己添加后台事件(Gridview双击事件)
- 关于如何给C#中的ListBox控件添加双击事件
- VS2010不能添加变量 双击不能添加处理事件 以及如何添加窗口系统事件
- 为.net中的ListBox控件添加双击事件-.NET教程,组件控件开发
- Silverlight开发中的疑难杂症-控件设计篇-如何实现一个NumericBox(上)
- unity开发之八:如何在UGUI其他的组件上添加点击事件(解答)
- 【VS开发】动态添加的ActiveX控件如何响应事件
- Silverlight开发中的疑难杂症-控件设计篇-如何实现一个NumericBox(下)
- 如何在natTable表格上添加双击事件
- 最简单的silverlight的DataGrid行双击事件添加
- 【iOS开发】如何给字符串添加超连接(响应事件)
- 如何自己添加后台事件(grvMain双击事件)
- 【百度地图API】如何给自定义覆盖物添加事件
- QuickCSharp框架开发(20)------授权部分的代码以及如何使用授权 添加用户、角色与分配资源部分的代码暂且省略
- silverlight 自定义 鼠标 双击事件
- iPhone开发之区分 touch 事件是单击还是双击的方法