ArcGIS API for Silverlight 点击地图弹出自定义窗体

本文介绍如何使用Silverlight创建一个可自定义样式的地图弹出窗体,并在地图上点击点时实例化该窗体。通过实例代码展示如何通过Grid布局和Canvas事件处理来实现弹出窗体的显示、关闭以及响应外部点击事件。
摘要由CSDN通过智能技术生成
在之前的博客中,关于弹出窗体,样式比较单一,不能满足自定义各种样式,源连接地址:http://blog.csdn.net/taomanman/article/details/7333612

  下面是通过新建一个Silverlight的UserControl类,里面自定义样式,只要你能想到的,都可以做出来,然后在地图中点击点,实例化该类即可,主要代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MapClient.ServiceReference1;
using System.Windows.Browser;
using System.Windows.Controls.Primitives;
using ESRI.ArcGIS.Client;

namespace MapClient
{
    public partial class MapTip : UserControl
    {
        string _placeName = string.Empty; //监测点名称
        string _areaID = string.Empty; //地区编号
        string _monitorID = string.Empty; //监测点ID
        string _year = string.Empty; //年份
        string _issueNum = string.Empty; //期号
        string _cycleID = string.Empty; //生育周期ID
        string _seedingTypeID = string.Empty; //苗情等级ID

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

        //初始化并显示弹出窗体.公共方法在显示菜单项时调用
        public void Show(Point location)
        {
            if (_isShowing)
                throw new InvalidOperationException();
            _isShowing = true;
            _location = location;
            ConstructPopup();
            _popup.IsOpen = true;
        }

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

        //Default behavior for OnClickOutside() is to close the context menu when there is a mouse click event outside the context menu
        protected virtual void OnClickOutside()
        {
            Close();
        }

        // 用Grid来布局,初始化弹出窗体
        //在Grid里面添加一个Canvas,用来监测菜单项外面的鼠标点击事件
        private void ConstructPopup()
        {
            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 = this;
            _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;
            }
        }

        public MapTip(string name, string areaID, string monitorID, string year, string cycleID, string issueNum, string seedingTypeID)
        {
            InitializeComponent();
            this._placeName = name;
            this._areaID = areaID;
            this._monitorID = monitorID;
            this._issueNum = issueNum;
            this._year = year;
            this._cycleID = cycleID;
            this._seedingTypeID = seedingTypeID;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            getData1SoapClient client = new getData1SoapClient();
            client.GetMonitorInfoByIssueNumCompleted += new EventHandler<GetMonitorInfoByIssueNumCompletedEventArgs>(client_GetMonitorInfoByIssueNumCompleted);
            client.GetMonitorInfoByIssueNumAsync(_areaID, _monitorID, _year, _issueNum, _cycleID);
        }

        void client_GetMonitorInfoByIssueNumCompleted(object sender, GetMonitorInfoByIssueNumCompletedEventArgs e)
        {
            try
            {
                string result = e.Result;
                if (result.Split('|').Length == 1)
                {
                    this.tb_title.Text = _placeName;
                    this.tb_zjyl.Text = result.Split('|')[0].ToString();
                    this.tb_zjyl.Foreground = new SolidColorBrush(Colors.Red);
                    this.tb_zjyl.HorizontalAlignment = HorizontalAlignment.Center;
                }
                else
                {
                    this.tb_title.Text = result.Split('|')[0].ToString(); //标题
                    this.tb_grade.Text = result.Split('|')[1].ToString(); //所属类型
                    this.tb_area.Text = result.Split('|')[2].ToString(); //代表面积
                    this.tb_variety.Text = result.Split('|')[3].ToString(); //品种
                    this.tb_varietyType.Text = result.Split('|')[4].ToString(); //品种类型
                    switch (_cycleID)
                    {
                        case "5":
                            //越冬期
                            this.tb_zjyl.Text = result.Split('|')[5].ToString(); //主茎叶龄
                            this.tb_dzjn.Text = result.Split('|')[6].ToString(); //单株茎蘖
                            this.tb_csg.Text = result.Split('|')[7].ToString(); //次生根
                            this.tb_mjn.Text = result.Split('|')[8].ToString(); //亩茎蘖
                            break;
                        case "6":
                            //返青期
                            this.tb_zjyl.Text = result.Split('|')[5].ToString(); //主茎叶龄
                            this.tb_dzjn.Text = result.Split('|')[6].ToString(); //单株茎蘖
                            this.tb_csg.Text = result.Split('|')[7].ToString(); //次生根
                            this.tb_mjn.Text = result.Split('|')[8].ToString(); //亩茎蘖
                            break;
                        case "8":
                            //拔节期
                            this.tb_zjyl.Text = result.Split('|')[5].ToString(); //主茎叶龄
                            this.tb_dzjn.Text = result.Split('|')[6].ToString(); //单株茎蘖
                            this.tb_csg.Text = result.Split('|')[7].ToString(); //亩茎蘖
                            break;
                        case "10":
                            //抽穗期
                            this.tb_zjyl.Text = result.Split('|')[5].ToString(); //主茎叶龄
                            this.tb_dzjn.Text = result.Split('|')[6].ToString(); //亩茎蘖
                            break;
                    }
                    //苗情评价分析
                    btn1.Content = result.Split('|')[0].ToString().Split(')')[0] + ")苗情评价信息";
                    //苗情数据报表
                    btn2.Content = result.Split('|')[0].ToString().Split(')')[0] + ")小麦苗情数据报表";
                    //苗情监测报告
                    btn3.Content = result.Split('|')[0].ToString().Split(')')[0] + ")苗情监测报告";
                }
            }
            catch (Exception)
            {
                this.tb_title.Text = _placeName;
                this.tb_zjyl.Text = "暂无数据!";
                this.tb_zjyl.Foreground = new SolidColorBrush(Colors.Red);
                this.tb_zjyl.HorizontalAlignment = HorizontalAlignment.Center;
            }
        }

        void btn1_Click(object sender, RoutedEventArgs e)
        {
            //页面跳转后并关闭当前弹出窗体
            try
            {
                ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeMenuName"as ScriptObject;
                string firstMenuName = "农情分析";
                string secondMenuName = "苗情评价";
                string menuName = "GIS地图监测点小麦苗情评价";
                string str = "?pAreaType=&pCounty=" + _areaID + "&pYear=" + _year + "&pPeriod=" + _issueNum + "&pStageID=" + _cycleID + "&pSeedlingType=" + 
_seedingTypeID + "&pMonitorID=" + _monitorID;
                SetMaterial.InvokeSelf(firstMenuName, secondMenuName, menuName, str);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            Close();
        }

        void btn2_Click(object sender, RoutedEventArgs e)
        {
            //页面跳转后并关闭当前弹出窗体
            ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情分析";
            string secondMenuName = "苗情分布统计分析";
            string thirdMenuName = "苗情分布统计";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }

        void btn3_Click(object sender, RoutedEventArgs e)
        {
            //页面跳转后并关闭当前弹出窗体
            ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情管理";
            string secondMenuName = "信息录入";
            string thirdMenuName = "小麦苗情监测报告";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }
  private void tb_zjhy_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //专家会议
            ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情监控";
            string secondMenuName = "农情监控";
            string thirdMenuName = "农田监控";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }

        private void tb_xcld_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //现场联动
            ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情监控";
            string secondMenuName = "农情监控";
            string thirdMenuName = "农田图像监控";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }

        private void tb_tjsp_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
          // 田间视频
      ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情监控";
            string secondMenuName = "农情监控";
            string thirdMenuName = "农田图像监控";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }

        private void tb_zwtp_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
          // 作物图片
      ScriptObject SetMaterial = HtmlPage.Window.GetProperty("GotoHomeUrlStr"as ScriptObject;
            string firstMenuName = "农情监控";
            string secondMenuName = "农情监控";
            string thirdMenuName = "农田图像监控";
            string str = "?AreaName=" + _placeName;
            SetMaterial.InvokeSelf(firstMenuName, secondMenuName, thirdMenuName, str);
            Close();
        }

    }
}

         #region 鼠标左键事件开始
         void Feature_MouseLeftButtonUp( object sender, MouseButtonEventArgs e)
        {
             //鼠标左键,显示ToolTip信息
            Graphic g = sender  as Graphic;
             for ( int i = 0; i < areaId.Length; i++)
            {
                 if (areaId[i] == g.Attributes[ "Id"].ToString())
                {
                     //弹出对应的提示信息
                    MapTip tip =  new MapTip(g.Attributes[ "Name"].ToString(), g.Attributes[ "AreaID"].ToString(), areaId[i], year[i], cycleId[i], issueNum[i], seedlingType[i]);
                    Point p = e.GetPosition(LayoutRoot);
                    p.X = p.X - 220;
                    p.Y = p.Y + 80;
                    tip.Show(p);
                }
            }
        }

  效果如下图所示:

  

  本文来自taomanman的博客,原文地址:http://blog.csdn.net/taomanman/article/details/7469594

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值