ArcGIS API for Silverlight 鼠标移入移出地图要素弹出窗口(优化处理)

简介:      在之前博客里的ArcGIS API for Silverlight 弹出框实例中,是通过点击地图要素,弹出框,但是由于没有控制元素个数,只是通过显示隐藏来进行的话,在鼠标移入和移出操作中,会出现鼠标移入的时候,总不能立刻弹出框,而是需要多次才行,用户体验较差,现在通过控制加入一个弹出框,移出时去除刚加入的弹出框,严格控制弹出框个数来实现。

     在之前博客里的ArcGIS API for Silverlight 弹出框实例中,是通过点击地图要素,弹出框,但是由于没有控制元素个数,只是通过显示隐藏来进行的话,在鼠标移入和移出操作中,会出现鼠标移入的时候,总不能立刻弹出框,而是需要多次才行,用户体验较差,现在通过控制加入一个弹出框,移出时去除刚加入的弹出框,严格控制弹出框个数来实现。


核心代码如下:

//鼠标移入事件
 graphic.MouseEnter += new MouseEventHandler(swz_graphic_MouseEnter);
 graphic.MouseLeave += new MouseEventHandler(swz_graphic_MouseLeave);

void swz_graphic_MouseEnter(object sender, MouseEventArgs e)
{
            Point p = e.GetPosition(LayoutRoot);
            p.X = p.X - 40;
            p.Y = p.Y - 165;

            //鼠标左键,显示ToolTip信息
            Graphic g = sender as Graphic;
            tip_Base.g_TipSW_LeftBottom = new TipSW_LeftBottom(this, p, g.Attributes["SWZMC"].ToString(), g.Attributes["SWZBM"].ToString());
}


 void swz_graphic_MouseLeave(object sender, MouseEventArgs e)
{
            tip_Base.g_TipSW_LeftBottom.closeWindow(this);
}
public partial class TipSW_LeftBottom : UserControl
{
        MainPage mp;
        string l_name;
        string l_swzbm;


        public TipSW_LeftBottom()
        {
            InitializeComponent();
        }


        public TipSW_LeftBottom(MainPage mp, Point p, string name, string swzbm)
        {
            InitializeComponent();
            this.mp = mp;


            this.l_name = name;
            this.l_swzbm = swzbm;


            //处理标题的间距问题
            string tmp = name;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tmp.Length; i++)
            {
                sb.Append(tmp[i] + " ");
            }
            this.title.Content = sb.ToString();
            //绑定洪水预报的数据
            getDataSoapClient client = new getDataSoapClient();
            client.getHSYBInfoCompleted += new EventHandler<getHSYBInfoCompletedEventArgs>(client_getHSYBInfoCompleted);
            client.getHSYBInfoAsync(name.Trim());


            getXQYJInfoSoapClient client2 = new getXQYJInfoSoapClient();
            client2.GetAllSWZCompleted += new EventHandler<GetAllSWZCompletedEventArgs>(client_GetAllSWZCompleted);
            client2.GetAllSWZAsync();


            this.Margin = new Thickness(p.X, p.Y, 0, 0);
            mp.LayoutRoot.Children.Add(this);
        }




        #region 通用方法,只需要更改Show,在实例化窗体的时候,传入不同的参数即可


        private Point _location;
        private bool _isShowing;
        private Popup _popup;
        private Grid _grid;
        private Canvas _canvas;
        private FrameworkElement _content;


        //初始化并显示弹出窗体.公共方法在显示菜单项时调用
        public void Show(Point location, string name,string swzbm)
        {
            this.l_name = name;
            this.l_swzbm = swzbm;


            if (_isShowing)
                throw new InvalidOperationException();
            _isShowing = true;
            _location = location;
            ConstructPopup(this);
            _popup.IsOpen = true;


            //处理标题的间距问题
            string tmp = name;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tmp.Length; i++)
            {
                sb.Append(tmp[i] + " ");
            }
            this.title.Content = sb.ToString();
            //绑定洪水预报的数据
            getDataSoapClient client = new getDataSoapClient();
            client.getHSYBInfoCompleted += new EventHandler<getHSYBInfoCompletedEventArgs>(client_getHSYBInfoCompleted);
            client.getHSYBInfoAsync(name.Trim());
            //水位站数据
            getXQYJInfoSoapClient client2 = new getXQYJInfoSoapClient();
            client2.GetAllSWZCompleted += new EventHandler<GetAllSWZCompletedEventArgs>(client_GetAllSWZCompleted);
            client2.GetAllSWZAsync();
        }


        public void Show(Point location)
        {
            if (_isShowing)
                throw new InvalidOperationException();
            _isShowing = true;
            _location = location;
            ConstructPopup(this);
            _popup.IsOpen = true;
        }


        //关闭弹出窗体
        public void Close()
        {
            _isShowing = false;
            if (_popup != null)
            {
                _popup.IsOpen = false;
            }
        }


        //弹出框外面点击则关闭该窗口
        protected virtual void OnClickOutside()
        {
            Close();
        }


        // 用Grid来布局,初始化弹出窗体
        //在Grid里面添加一个Canvas,用来监测菜单项外面的鼠标点击事件
        private void ConstructPopup(FrameworkElement _element)
        {
            if (_popup != null)
                return;
            _popup = new Popup();
            _grid = new Grid();
            _popup.Child = _grid;
            _canvas = new Canvas();
            _canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
            _canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };
            _canvas.Background = new SolidColorBrush(Colors.Transparent);
            _grid.Children.Add(_canvas);
            _content = _element;
            _content.HorizontalAlignment = HorizontalAlignment.Left;
            _content.VerticalAlignment = VerticalAlignment.Top;
            _content.Margin = new Thickness(_location.X, _location.Y, 0, 0);
            _grid.Children.Add(_content);
            UpdateSize();
        }


        /// <summary>
        /// 更新大小
        /// </summary>
        private void UpdateSize()
        {
            _grid.Width = Application.Current.Host.Content.ActualWidth;
            _grid.Height = Application.Current.Host.Content.ActualHeight;
            if (_canvas != null)
            {
                _canvas.Width = _grid.Width;
                _canvas.Height = _grid.Height;
            }
        }


        #endregion


        #region WebService 调用方法及关闭窗体方法
        void client_GetAllSWZCompleted(object sender, GetAllSWZCompletedEventArgs e)
        {
            try
            {
                ObservableCollection<RiverFall> lists = e.Result;
                foreach (RiverFall item in lists)
                {
                    if (item.SWZBM == l_swzbm)
                    {
                        this.dt.Content = item.DTNow.ToString("yyyy年M月d日H时");
                        this.sw.Content = "水位:" + item.SW.ToString("N2") + "m";
                    }
                }
            }
            catch (Exception ex)
            {
                this.dt.Content = "没有监测到该站点数据";
                this.sw.Content = "";
            }
        }


        void client_getHSYBInfoCompleted(object sender, getHSYBInfoCompletedEventArgs e)
        {
            try
            {
                IList<洪水预报> ret = e.Result;
                if (ret.Count > 0)
                {
                    foreach (洪水预报 r in ret)
                    {
                        this.tbhsyb.Text = r.标题.Length > 9 ? r.标题.Substring(0, 7) + "..." : r.标题;
                        this.tbhsyb.Foreground = new SolidColorBrush(Colors.Red);
                    }
                }
                else
                {
                    this.tbhsyb.Text = "暂无预报";
                    this.tbhsyb.Foreground = new SolidColorBrush(Colors.Black);
                }
            }
            catch
            {
            }
        }

        //用于移除刚加入的弹出框,在主窗体鼠标移出后,调用此方法
        public void closeWindow(MainPage mp)
        {
             mp.LayoutRoot.Children.RemoveAt(mp.LayoutRoot.Children.Count - 1);
        }
        #endregion

    }
这里代码中仍然保留了原先的点击弹出框的Show方法,可以根据需要调用Show方法或直接实例化带参数的构造函数都可以。

相关文章
|
8天前
|
JSON 监控 API
Shopee:对接海外仓API实现本地发货,优化物流时效
Shopee卖家可通过对接海外仓API实现本地发货,将物流时效从10-15天缩短至3-5天,显著提升买家体验与店铺转化率。本文详解API对接原理、步骤及代码示例,助力优化跨境物流效率。
45 1
|
12天前
|
JSON 监控 API
亚马逊:调用跨境物流API追踪国际包裹清关状态,优化时效
在亚马逊跨境运营中,清关不确定性常导致物流延误。通过调用跨境物流API(如Amazon SP-API),可自动化获取包裹清关状态与预计交付时间,提升响应效率。本文详解API调用步骤,提供Python代码示例,并分享实时监控、预警机制与数据优化策略,助力卖家缩短处理时间、提升客户满意度,实现高效智能的国际物流管理。
51 0
|
16天前
|
数据采集 API
京东:调用用户行为API分析购买路径,优化页面跳转逻辑
京东通过整合用户行为API,构建购买路径分析体系,运用马尔可夫链模型识别高损耗、断裂与冗余路径,优化页面跳转逻辑。实施流程合并、预加载及实时干预策略,转化率提升30.2%,路径缩短34.9%,跳转失败率下降78.7%,实现数据驱动的精细化运营。
178 0
|
16天前
|
JSON 数据可视化 API
淘宝/天猫:利用销售数据API生成区域热力图,优化仓储布局
本文详解如何利用淘宝/天猫销售数据API生成区域热力图,结合核密度估计与线性规划,科学优化仓储布局。通过数据驱动降低物流成本15%-20%,提升配送效率,助力电商高效运营。(238字)
126 0
|
17天前
|
存储 监控 前端开发
淘宝商品详情 API 实战:5 大策略提升店铺转化率(附签名优化代码 + 避坑指南)
本文深入解析淘宝商品详情API的核心字段与实战应用,分享如何通过动态定价、库存预警、差评控制等5大策略提升电商转化率。结合300+店铺实战经验,提供优化代码与避坑指南,助力开发者与运营者实现数据驱动的精细化运营。
机器学习/深度学习 搜索推荐 算法
39 0
|
24天前
|
机器学习/深度学习 自然语言处理 算法
小红书:通过商品标签API自动生成内容标签,优化社区推荐算法
小红书通过商品标签API自动生成内容标签,提升推荐系统精准度与用户体验。流程包括API集成、标签生成算法与推荐优化,实现高效率、智能化内容匹配,助力社交电商发展。
52 0
|
26天前
|
JSON 算法 API
京东:利用商品管理API自动调整商品上下架状态,优化搜索排名
京东通过商品管理API实现商品状态自动调整,优化搜索排名。本文详解API功能、实现方法、排名优化原理,并提供Python代码示例,助力商家提升运营效率与曝光度。
37 0
|
1月前
|
供应链 算法 API
借助唯品会 API,唯品会店铺运动商品库存管理优化
本文介绍如何利用唯品会API优化运动商品库存管理。运动商品因季节性和促销影响,库存波动大,传统管理易导致缺货或积压。通过唯品会API,可实现库存实时监控、销售数据同步与自动化补货。结合预测模型与API数据,店铺可降低成本、提升效率,并增强市场响应能力,实现智能化运营。
45 0
|
2月前
|
供应链 搜索推荐 API
苏宁易购 API 助力,苏宁易购平台 3C 产品销售策略优化
在电商竞争激烈的环境下,苏宁易购通过API优化3C产品销售策略,实现数据驱动决策、个性化推荐与智能库存管理,提升销售额与运营效率,增强用户体验,巩固市场竞争力。
56 0

热门文章

最新文章