ArcGIS API for Silverlight代碼中使用Template模板

來源:互聯網
上載者:User

標籤:

原文:ArcGIS API for Silverlight代碼中使用Template模板

       在項目開發中,會遇到點選中聚焦閃爍效果,但是因為在使用Symbol的時候,會設定一定的OffSetX和OffSetY,所以聚焦閃爍的時候,有些情況下,會出現閃爍點的位置和Symbol的位置不重疊現象,下面的方法就是解決這個問題的。

1、在Silverlight項目中建立一個檔案夾Template,建立一個DefaultMarkerSymbol.xaml的Silverlight資源字典檔案,如:



2、開啟DefaultMarkerSymbol.xaml檔案,刪掉裡面的內容,拷貝如下的內容:

<ControlTemplate    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"    >    <Canvas>        <vsm:VisualStateManager.VisualStateGroups>            <vsm:VisualStateGroup x:Name="CommonStates">                <vsm:VisualState x:Name="Normal">                    <Storyboard RepeatBehavior="Forever">                        <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" From="1" To="5" Duration="00:00:01" />                        <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" From="1" To="5" Duration="00:00:01" />                        <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)" From="1" To="0" Duration="00:00:01" />                    </Storyboard>                </vsm:VisualState>            </vsm:VisualStateGroup>        </vsm:VisualStateManager.VisualStateGroups>        <Ellipse Height="25" Width="25" Canvas.Left="0" Canvas.Top="0" RenderTransformOrigin="0.5,0.5" x:Name="ellipse" IsHitTestVisible="False">            <Ellipse.RenderTransform>                <ScaleTransform />            </Ellipse.RenderTransform>            <Ellipse.Fill>                <RadialGradientBrush>                    <GradientStop Color="#00FF0000" />                    <GradientStop Color="#FFFF0000" Offset="0.25" />                    <GradientStop Color="#00FF0000" Offset="0.5" />                    <GradientStop Color="#FFFF0000" Offset="0.75" />                    <GradientStop Color="#00FF0000" Offset="1" />                </RadialGradientBrush>            </Ellipse.Fill>        </Ellipse>        <Ellipse Height="20" Width="20" Canvas.Left="0" Canvas.Top="0" Fill="#FFFF0000" x:Name="ellipse1" />    </Canvas></ControlTemplate >

3、Silverlight程式碼中使用,這裡是通過選中ListBox中的網站名稱,進行聚焦顯示,方法如下:

 private void lbSearchSite_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)        {            RainFall findResult = e.AddedItems[0] as RainFall;            Graphic g = new Graphic()            {                Geometry = mercator.FromGeographic(new MapPoint(double.Parse(findResult.Latitute.ToString().Trim()), double.Parse(findResult.Longitute.ToString().Trim())))            };            MarkerSymbol symbol = new MarkerSymbol();            string contentStr = new StreamReader(                         Application.GetResourceStream(                                  new Uri("/MapClient;component/Template/DefaultMarkerSymbol.xaml", UriKind.Relative)                                  ).Stream).ReadToEnd();            symbol.ControlTemplate = (ControlTemplate)XamlReader.Load(contentStr);            symbol.OffsetX = 10;            symbol.OffsetY = 10;            g.Symbol = symbol;            ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = mercator.ToGeographic(g.Geometry).Extent; //選中點的位置            double expandPercentage = 10;            //加數值後,聚焦(這裡需要注意,進行地理座標和墨卡托座標的轉換)            double widthExpand = (selectedFeatureExtent.Width + 5) * (expandPercentage / 100);            double heightExpand = (selectedFeatureExtent.Height + 5) * (expandPercentage / 100);            ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2))), WKIDConvert.lonlat2mercator(new MapPoint(selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))))            {                SpatialReference = new SpatialReference(102100)            };            try            {                //聚焦                myMap.ZoomTo(displayExtent);                ShowFocus(g);            }            catch (Exception)            {            }        }        /// <summary>        /// 顯示聚焦點        /// </summary>        public void ShowFocus(Graphic g)        {            GraphicsLayer graphicsLayerSW = myMap.Layers["GraphicsLayerRed"] as GraphicsLayer;            //產生紅色光暈            if (graphicsLayerSW.Graphics.Count < 1)            {                graphicsLayerSW.Graphics.Add(g);            }            else            {                graphicsLayerSW.Graphics.RemoveAt(0);                graphicsLayerSW.Graphics.Add(g);            }        }


4、如下:



ArcGIS API for Silverlight代碼中使用Template模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.