在之前博客里的ArcGIS API for Silverlight d出框实例中,是通过点击地图要素,d出框,但是由于没有控制元素个数,只是通过显示隐藏来进行的话,在鼠标移入和移出 *** 作中,会出现鼠标移入的时候,总不能立刻d出框,而是需要多次才行,用户体验较差,现在通过控制加入一个d出框,移出时去除刚加入的d出框,严格控制d出框个数来实现。
核心代码如下:
//鼠标移入事件 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); 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; //初始化并显示d出窗体.公共方法在显示菜单项时调用 public voID Show(Point location,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; } //关闭d出窗体 public voID Close() { _isShowing = false; if (_popup != null) { _popup.IsOpen = false; } } //d出框外面点击则关闭该窗口 protected virtual voID OnClickOutsIDe() { Close(); } // 用GrID来布局,初始化d出窗体 //在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); _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 { } } //用于移除刚加入的d出框,在主窗体鼠标移出后,调用此方法 public voID closeWindow(MainPage mp) { mp.LayoutRoot.Children.RemoveAt(mp.LayoutRoot.Children.Count - 1); } #endregion }这里代码中仍然保留了原先的点击d出框的Show方法,可以根据需要调用Show方法或直接实例化带参数的构造函数都可以。 总结
以上是内存溢出为你收集整理的ArcGIS API for Silverlight 鼠标移入移出地图要素d出窗口(优化处理)全部内容,希望文章能够帮你解决ArcGIS API for Silverlight 鼠标移入移出地图要素d出窗口(优化处理)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)