Windows Phone 7 让所有容器 轻松拥有自定义事件
举例实现以下事件:
/// <summary>
/// 弹起后鼠标位置
/// </summary>
private MouseButtonEventArgs _mouseUpMouseButtonEventArgs;
/// <summary>
/// 单击鼠标左键时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Click;
/// <summary>
/// 单击鼠标右键事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Right_Click;
/// <summary>
/// 双击鼠标左键时触发该事件。
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Double_Click;
/// <summary>
/// 鼠标拖拽时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Drag_Start;
/// <summary>
/// 鼠标拖拽过程中触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Dragging;
/// <summary>
/// 鼠标拖拽结束时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Drag_End;
具体实现如下:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MapEngine.Events
{
/// <summary>
/// FrameworkElement相关事件
/// </summary>
public class FrameworkElementEvents
{
/// <summary>
/// 事件注册的容器
/// </summary>
private readonly FrameworkElement _frameworkElement;
/// <summary>
/// 鼠标是否在容器上
/// </summary>
private bool _isMouseOver;
/// <summary>
/// 是否激活事件
/// </summary>
private bool IsActive { get; set; }
/// <summary>
/// 最后点击的位置时间戳
/// </summary>
private DateTime _lastClick;
/// <summary>
/// 是否点击
/// </summary>
private bool _isMouseDown;
/// <summary>
/// 是否拖拽
/// </summary>
private bool _isMouseDrag;
/// <summary>
/// 鼠标双击速度间隔
/// </summary>
private const double MouseDoubleClickSpeed = 3000000;
private const double MouseDragDelay = 2000000.0;
/// <summary>
/// 单击速度
/// </summary>
private const double MouseClickSpeed = 2000000.0;
/// <summary>
/// 点击偏移量
/// </summary>
private const double MouseClickOffsetX = 10E-7;
/// <summary>
/// 点击偏移量
/// </summary>
private const double MouseClickOffsetY = 10E-7;
/// <summary>
/// 拖拽偏移量
/// </summary>
private const double MouseDragOffsetX = 10E-1;
/// <summary>
/// 拖拽偏移量
/// </summary>
private const double MouseDragOffsetY = 10E-1;
private Storyboard _storyboardOnFrame;
/// <summary>
/// 最后点击的位置
/// </summary>
private Point _lastClickPos;
/// <summary>
/// 单击后是否悬停等待下一个动作
/// </summary>
private bool _pendingClick = false;
/// <summary>
/// 是否正在拖拽
/// </summary>
private bool _dragging = false;
/// <summary>
/// 弹起后鼠标位置
/// </summary>
private MouseButtonEventArgs _mouseUpMouseButtonEventArgs;
/// <summary>
/// 单击鼠标左键时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Click;
/// <summary>
/// 单击鼠标右键事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Right_Click;
/// <summary>
/// 双击鼠标左键时触发该事件。
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Double_Click;
/// <summary>
/// 鼠标拖拽时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Drag_Start;
/// <summary>
/// 鼠标拖拽过程中触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Dragging;
/// <summary>
/// 鼠标拖拽结束时触发该事件
/// </summary>
public event EventHandler<MouseEventArgs> Mouse_Drag_End;
/// <summary>
/// 触发鼠标拖拽事件
/// </summary>
/// <param name="args"></param>
private void FireMouse_Dragging(MouseEventArgs args)
{
if (this.Mouse_Dragging != null)
{
Mouse_Dragging(_frameworkElement, args);
}
} /// <summary>
/// 触发双击鼠标左键事件
/// </summary>
/// <param name="args"></param>
private void FireMouse_Double_Click(MouseEventArgs args)
{
if (this.Mouse_Double_Click != null)
{
Mouse_Double_Click(_frameworkElement, args);
}
}
/// <summary>
/// 触发开始拖拽事件
/// </summary>
/// <param name="args"></param>
private void FireMouseDragStart(MouseEventArgs args)
{
if (this.Mouse_Drag_Start != null)
{
Mouse_Drag_Start(_frameworkElement, args);
}
}
/// <summary>
/// 触发单击鼠标右键事件
/// </summary>
/// <param name="args"></param>
private void FireMouse_Right_Click(MouseEventArgs args)
{
if (this.Mouse_Right_Click != null)
{
Mouse_Right_Click(_frameworkElement, args);
}
}
/// <summary>
/// 触发单击鼠标左键事件
/// </summary>
/// <param name="args"></param>
private void FireMouse_Click(MouseEventArgs args)
{
if (this.Mouse_Click != null)
{
Mouse_Click(_frameworkElement, args);
}
}
/// <summary>
/// 触发鼠标拖拽结束事件
/// </summary>
/// <param name="args"></param>
private void FireMouse_Drag_End(MouseEventArgs args)
{
if (this.Mouse_Drag_End != null)
{
Mouse_Drag_End(_frameworkElement, args);
}
}
///// <summary>
///// 是否允许鼠标滚轮
///// </summary>
//public bool IsWheelEnabled { get; set; }
/// <summary>
/// 构造函数,需要地图实例或 容器
/// </summary>
public FrameworkElementEvents(FrameworkElement fe)
{
_frameworkElement = fe;
IsActive = false;
}
/// <summary>
/// 激活事件
/// </summary>
public void Active()
{
if (!IsActive)
{
if (_storyboardOnFrame == null)
{
this._storyboardOnFrame = new Storyboard();
}
this._storyboardOnFrame.Duration += new Duration(new TimeSpan(0, 0, 0, 0, 0));
this._storyboardOnFrame.Completed += new EventHandler(this.StoryboardOnFrame_Completed);
_frameworkElement.MouseLeftButtonDown += new MouseButtonEventHandler(_Map_MouseLeftButtonDown);
_frameworkElement.MouseLeftButtonUp += new MouseButtonEventHandler(_Map_MouseLeftButtonUp);
_frameworkElement.MouseMove += new MouseEventHandler(_Map_MouseMove);
_frameworkElement.MouseEnter += new MouseEventHandler(_MouseEnter);
_frameworkElement.MouseLeave += new MouseEventHandler(_MouseLeave);
IsActive = true;
}
}
/// <summary>
/// 注销事件
/// </summary>
public void DeActive()
{
if (IsActive)
{
if (_storyboardOnFrame != null)
{
this._storyboardOnFrame.Stop();
_storyboardOnFrame = null;
}
_frameworkElement.MouseLeftButtonDown -= new MouseButtonEventHandler(_Map_MouseLeftButtonDown);
_frameworkElement.MouseLeftButtonUp -= new MouseButtonEventHandler(_Map_MouseLeftButtonUp);
_frameworkElement.MouseMove -= new MouseEventHandler(_Map_MouseMove);
_frameworkElement.MouseEnter -= new MouseEventHandler(_MouseEnter);
_frameworkElement.MouseLeave -= new MouseEventHandler(_MouseLeave);
IsActive = false;
}
}
#region 鼠标事件
/// <summary>
/// 鼠标左键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _Map_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!e.Handled)
{
_frameworkElement.CaptureMouse();
_isMouseDown = true;//鼠标按下
this._pendingClick = false;
if ((DateTime.Now.Ticks - _lastClick.Ticks) < MouseDoubleClickSpeed)
{
this._pendingClick = false;
if (Math.Abs(e.GetPosition(_frameworkElement).X - _lastClickPos.X) < 5 && Math.Abs(e.GetPosition(_frameworkElement).Y - _lastClickPos.Y) < 5)
{
Point viewportPoint = e.GetPosition(_frameworkElement);
FireMouse_Double_Click(e);
}
this._lastClick = DateTime.MinValue;
}
else
{
this._lastClick = DateTime.Now;
}
_lastClickPos = e.GetPosition(_frameworkElement);
this._storyboardOnFrame.Begin();
}
}
/// <summary>
/// 鼠标移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _Map_MouseMove(object sender, MouseEventArgs e)
{
RaiseIfMouseDrag(e);
_isMouseDrag = true;
}
/// <summary>
/// 鼠标左键抬起
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _Map_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_frameworkElement.ReleaseMouseCapture();
_mouseUpMouseButtonEventArgs = e;
TimeSpan span = (TimeSpan)(DateTime.Now - this._lastClick);
if (span.TotalSeconds < 10.0 && !this._dragging)
{
this._pendingClick = true;
this._storyboardOnFrame.Begin();
}
if (_isMouseDown && _isMouseDrag)
{
if (this._dragging)
{
Point viewportPoint = e.GetPosition(_frameworkElement);
FireMouse_Drag_End(e);
}
_dragging = false;
}
_isMouseDown = false;
_isMouseDrag = false;
}
/// <summary>
/// 定时检测鼠标事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StoryboardOnFrame_Completed(object sender, EventArgs e)
{
this.Update();
}
/// <summary>
/// 更新鼠标动作
/// </summary>
private void Update()
{
DateTime now = DateTime.Now;
if (this._pendingClick)
{
TimeSpan span = (TimeSpan)(now - this._lastClick);
if (span.TotalSeconds >= 0.3 && _mouseUpMouseButtonEventArgs != null)
{
this._pendingClick = false;
this.RaiseIfMouseClick(_mouseUpMouseButtonEventArgs);
}
this._storyboardOnFrame.Begin();
}
}
/// <summary>
/// 鼠标点击事件
/// </summary>
/// <param name="map"></param>
/// <param name="args"></param>
private void RaiseIfMouseClick(MouseButtonEventArgs args)
{
bool offsetX = Math.Abs(args.GetPosition(_frameworkElement).X - _lastClickPos.X) < MouseClickOffsetX;
bool offsetY = Math.Abs(args.GetPosition(_frameworkElement).Y - _lastClickPos.Y) < MouseClickOffsetY; ;
if (offsetX && offsetY)
{
Point viewportPoint = args.GetPosition(_frameworkElement);
FireMouse_Click(args);
}
}
/// <summary>
/// 地图拖拽事件
/// </summary>
/// <param name="map"></param>
/// <param name="args"></param>
private void RaiseIfMouseDrag(MouseEventArgs args)
{
if (_isMouseDown)
{
if (_lastClick.Ticks != 0)
{
double offsetX = args.GetPosition(_frameworkElement).X - _lastClickPos.X;
double offsetY = args.GetPosition(_frameworkElement).Y - _lastClickPos.Y;
if (_isMouseDown && Math.Abs(offsetX) > MouseDragOffsetX && Math.Abs(offsetY) > MouseDragOffsetY)
{
Point viewportPoint = args.GetPosition(_frameworkElement);
if (!_dragging)
{
FireMouseDragStart(args);
}
else
{
FireMouse_Dragging(args);
}
_dragging = true;
}
}
}
}
#endregion
/// <summary>
/// 鼠标移入
/// </summary>
/// <param name="map"></param>
/// <param name="args"></param>
private void _MouseEnter(object map, MouseEventArgs args)
{
_isMouseOver = true;
}
/// <summary>
/// 鼠标移出
/// </summary>
/// <param name="map"></param>
/// <param name="args"></param>
private void _MouseLeave(object map, MouseEventArgs args)
{
_isMouseDown = false;
_frameworkElement.ReleaseMouseCapture();
}
}
}
调用方式:
Grid grid = new Grid();
FrameworkElementEvents fl = new FrameworkElementEvents(grid);
fl.Active();
fl.Mouse_Click += new EventHandler<MouseEventArgs>(fl_Mouse_Click);
void fl_Mouse_Click(object sender, MouseEventArgs e)
{
MessageBox.Show(sender+"单击实现了!");
}
更多事件可以自己封装!