设为首页 收藏本站
查看: 882|回复: 0

Windows Phone 7 让所有容器 轻松拥有自定义事件

[复制链接]

尚未签到

发表于 2015-5-11 09:25:33 | 显示全部楼层 |阅读模式
  举例实现以下事件:



        ///
        /// 弹起后鼠标位置
        ///
        private MouseButtonEventArgs _mouseUpMouseButtonEventArgs;
        ///
        /// 单击鼠标左键时触发该事件
        ///
        public event EventHandler Mouse_Click;
        ///
        /// 单击鼠标右键事件
        ///
        public event EventHandler Mouse_Right_Click;
        ///
        /// 双击鼠标左键时触发该事件。
        ///
        public event EventHandler Mouse_Double_Click;
        ///
        /// 鼠标拖拽时触发该事件
        ///
        public event EventHandler Mouse_Drag_Start;
        ///
        /// 鼠标拖拽过程中触发该事件
        ///
        public event EventHandler Mouse_Dragging;
        ///
        /// 鼠标拖拽结束时触发该事件
        ///
        public event EventHandler 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
{
    ///
    /// FrameworkElement相关事件
    ///
    public class FrameworkElementEvents
    {
        ///
        /// 事件注册的容器
        ///
        private readonly FrameworkElement _frameworkElement;
        ///
        /// 鼠标是否在容器上
        ///
        private bool _isMouseOver;
        ///
        /// 是否激活事件
        ///
        private bool IsActive { get; set; }
        ///
        /// 最后点击的位置时间戳
        ///
        private DateTime _lastClick;
        ///
        /// 是否点击
        ///
        private bool _isMouseDown;
        ///
        /// 是否拖拽
        ///
        private bool _isMouseDrag;
        ///
        /// 鼠标双击速度间隔
        ///
        private const double MouseDoubleClickSpeed = 3000000;
        private const double MouseDragDelay = 2000000.0;
        ///
        /// 单击速度
        ///
        private const double MouseClickSpeed = 2000000.0;
        ///
        /// 点击偏移量
        ///
        private const double MouseClickOffsetX = 10E-7;
        ///
        /// 点击偏移量
        ///
        private const double MouseClickOffsetY = 10E-7;
        ///
        /// 拖拽偏移量
        ///
        private const double MouseDragOffsetX = 10E-1;
        ///
        /// 拖拽偏移量
        ///
        private const double MouseDragOffsetY = 10E-1;
        private Storyboard _storyboardOnFrame;
        ///
        /// 最后点击的位置
        ///
        private Point _lastClickPos;
        ///
        /// 单击后是否悬停等待下一个动作
        ///
        private bool _pendingClick = false;
        ///
        /// 是否正在拖拽
        ///
        private bool _dragging = false;
        ///
        /// 弹起后鼠标位置
        ///
        private MouseButtonEventArgs _mouseUpMouseButtonEventArgs;
        ///
        /// 单击鼠标左键时触发该事件
        ///
        public event EventHandler Mouse_Click;
        ///
        /// 单击鼠标右键事件
        ///
        public event EventHandler Mouse_Right_Click;
        ///
        /// 双击鼠标左键时触发该事件。
        ///
        public event EventHandler Mouse_Double_Click;
        ///
        /// 鼠标拖拽时触发该事件
        ///
        public event EventHandler Mouse_Drag_Start;
        ///
        /// 鼠标拖拽过程中触发该事件
        ///
        public event EventHandler Mouse_Dragging;
        ///
        /// 鼠标拖拽结束时触发该事件
        ///
        public event EventHandler Mouse_Drag_End;
        ///
        /// 触发鼠标拖拽事件
        ///
        ///
        private void FireMouse_Dragging(MouseEventArgs args)
        {
            if (this.Mouse_Dragging != null)
            {
                Mouse_Dragging(_frameworkElement, args);
            }
        } ///
        /// 触发双击鼠标左键事件
        ///
        ///
        private void FireMouse_Double_Click(MouseEventArgs args)
        {
            if (this.Mouse_Double_Click != null)
            {
                Mouse_Double_Click(_frameworkElement, args);
            }
        }
        ///
        /// 触发开始拖拽事件
        ///
        ///
        private void FireMouseDragStart(MouseEventArgs args)
        {
            if (this.Mouse_Drag_Start != null)
            {
                Mouse_Drag_Start(_frameworkElement, args);
            }
        }
        ///
        /// 触发单击鼠标右键事件
        ///
        ///
        private void FireMouse_Right_Click(MouseEventArgs args)
        {
            if (this.Mouse_Right_Click != null)
            {
                Mouse_Right_Click(_frameworkElement, args);
            }
        }
        ///
        /// 触发单击鼠标左键事件
        ///
        ///
        private void FireMouse_Click(MouseEventArgs args)
        {
            if (this.Mouse_Click != null)
            {
                Mouse_Click(_frameworkElement, args);
            }
        }
        ///
        /// 触发鼠标拖拽结束事件
        ///
        ///
        private void FireMouse_Drag_End(MouseEventArgs args)
        {
            if (this.Mouse_Drag_End != null)
            {
                Mouse_Drag_End(_frameworkElement, args);
            }
        }
        /////
        ///// 是否允许鼠标滚轮
        /////
        //public bool IsWheelEnabled { get; set; }
        ///
        /// 构造函数,需要地图实例或 容器
        ///
        public FrameworkElementEvents(FrameworkElement fe)
        {
            _frameworkElement = fe;
            IsActive = false;
        }
        ///
        /// 激活事件
        ///
        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;
            }
        }
        ///
        /// 注销事件
        ///
        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 鼠标事件
        ///
        /// 鼠标左键
        ///
        ///
        ///
        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();
            }
        }
        ///
        /// 鼠标移动
        ///
        ///
        ///
        void _Map_MouseMove(object sender, MouseEventArgs e)
        {
            RaiseIfMouseDrag(e);
            _isMouseDrag = true;
        }
        ///
        /// 鼠标左键抬起
        ///
        ///
        ///
        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;
        }
        ///
        /// 定时检测鼠标事件
        ///
        ///
        ///
        private void StoryboardOnFrame_Completed(object sender, EventArgs e)
        {
            this.Update();
        }
        ///
        /// 更新鼠标动作
        ///
        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();
            }
        }
        ///
        /// 鼠标点击事件
        ///
        ///
        ///
        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);
            }
        }
        ///
        /// 地图拖拽事件
        ///
        ///
        ///
        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
        ///
        /// 鼠标移入
        ///
        ///
        ///
        private void _MouseEnter(object map, MouseEventArgs args)
        {
            _isMouseOver = true;
        }
        ///
        /// 鼠标移出
        ///
        ///
        ///
        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(fl_Mouse_Click);


void fl_Mouse_Click(object sender, MouseEventArgs e)
        {
            MessageBox.Show(sender+"单击实现了!");
        }
  
  更多事件可以自己封装!
  

  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65699-1-1.html 上篇帖子: Windows Mobile 7 spotted in latest Live Mesh video 下篇帖子: Windows Phone 7的XNA游戏开发系列教程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表