Select to view content in your preferred language

Custom code behind Fill Symbol

979
4
03-10-2011 10:04 AM
FranciscoRodriguez1
Emerging Contributor
Hi,

I have this code line:

void AddPolygonGraphicsReport(string Coodinates)
        {
            GraphicsLayer PolygonGraphicsLayer = Mimapa.Layers["MyGraphicsLayer"] as GraphicsLayer;
            PointCollectionConverter pointConverter = new PointCollectionConverter();
            FillSymbol iFillSymbol = new FillSymbol
            {
                BorderBrush = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)),
                BorderThickness = 2,
                Fill = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0))
            };
            ESRI.ArcGIS.Client.Geometry.PointCollection PointCollectionReport =
            pointConverter.ConvertFromString(Coodinates) as ESRI.ArcGIS.Client.Geometry.PointCollection;
            ESRI.ArcGIS.Client.Geometry.Polygon PolygonReport = new ESRI.ArcGIS.Client.Geometry.Polygon();
            PolygonReport.Rings.Add(PointCollectionReport);
            Graphic GraphicPolygon = new Graphic()
            {
                Geometry = PolygonReport,
                Symbol = iFillSymbol
               
            };                          
                  PolygonGraphicsLayer.Graphics.Add(GraphicPolygon);
         
        }

But Silverlight can't show the type BorderBrush, BorderThickness and fill. Only obtain proporeties when write xaml:

<esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Green" Style="DashDot" Width="4" />

A same problem with SimpleLine:

SimpleLineSymbol iSimpleLineSymbol = new SimpleLineSymbol
            {
                Width = size,
                Color = new SolidColorBrush(SLGeoviewerClassLibrary.Model.Common.ArrayToColor(color)),
                Style = SLGeoviewerClassLibrary.Model.Common.sSimpleLineStyle(Convert.ToByte(style))
            };
            GraphicsLayer LineStringsGraphicsLayer = Mimapa.Layers["MyGraphicsLayer"] as GraphicsLayer;
            List<ESRI.ArcGIS.Client.Geometry.Polyline> polylineList = new List<ESRI.ArcGIS.Client.Geometry.Polyline>();
            ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection = new ESRI.ArcGIS.Client.Geometry.PointCollection();
            for (int i = 1; i < lsc.Length; i++)
            {
                string[] Coords = null;
                Coords = lsc.Split(' ');
                pointCollection.Add(new MapPoint(Convert.ToDouble(Coords[1], System.Globalization.CultureInfo.InvariantCulture),
                                                 Convert.ToDouble(Coords[2], System.Globalization.CultureInfo.InvariantCulture)));
            }
            ESRI.ArcGIS.Client.Geometry.Polyline polyline = new ESRI.ArcGIS.Client.Geometry.Polyline();
            polyline.Paths.Add(pointCollection);
            Graphic GraphicLineStrings = new Graphic();

            GraphicLineStrings.Geometry = polyline;
            GraphicLineStrings.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as Symbol;

            LineStringsGraphicsLayer.Graphics.Add(GraphicLineStrings);

Please help me.

Thanks
0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor
The following SimpleFillSymbols are equivalent:

XAML-code:
<esri:SimpleFillSymbol x:Key="RedFillSymbol" Fill="#66FF0000" BorderBrush="Red" BorderThickness="2" />


Code-behind:

SimpleFillSymbol redFillSymbol= new SimpleFillSymbol()
{
Fill = new SolidColorBrush(Color.FromArgb(102, 255, 0, 0)),
BorderBrush = new SolidColorBrush(Colors.Red),
BorderThickness = 2d
};


You can use either one to apply symbol on the polygon:
private void AddPolygon()
{
 GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;

 Polygon polygon = new Polygon();
 PointCollection pts = new PointCollection();
 pts.Add(new MapPoint(110.039, -20.303));
 pts.Add(new MapPoint(132.539, -7.0137));
 pts.Add(new MapPoint(153.281, -13.923));
 pts.Add(new MapPoint(162.773, -35.174));
 pts.Add(new MapPoint(133.594, -43.180));
 pts.Add(new MapPoint(111.797, -36.032));
 pts.Add(new MapPoint(110.039, -20.303));
 polygon.Rings.Add(pts);

 Graphic graphic = new Graphic()
 {
  Geometry = polygon,
  Symbol = redFillSymbol // or LayoutRoot.Resources["RedFillSymbol"] as Symbol
 };
 graphicsLayer.Graphics.Add(graphic);
}


Are you saying defining symbol in code-behind does not work?
0 Kudos
FranciscoRodriguez1
Emerging Contributor
Yes Jennifer,

I can't show  properties define on code behind of polygon and Line symbol. When define in XAML work's fine but don't  work when I define by code behind.

Work
<esri:SimpleFillSymbol x:Key="RedFillSymbol" Fill="#66FF0000" BorderBrush="Red" BorderThickness="2" />
Don't Work

SimpleFillSymbol redFillSymbol= new SimpleFillSymbol()
{
Fill = new SolidColorBrush(Color.FromArgb(102, 255, 0, 0)),
BorderBrush = new SolidColorBrush(Colors.Red),
BorderThickness = 2d
};
What can I do?

Friends
0 Kudos
FranciscoRodriguez1
Emerging Contributor
Dear Jennifer,

Thank i found the problem, I have function don't work fine:

public static double StringToDouble(string sText)
        {
            try
            {
                string sdecSep = (0.1).ToString().Substring(1, 1);
                return Convert.ToDouble(sText.Replace(".", ",").Replace(",", sdecSep));
            }
            catch (Exception)
            {
                return 0;
            }

        }
public static Color ArrayToColor(string[] Col)
        {
            try
            {
                return Color.FromArgb(
                    Convert.ToByte(Convert.ToInt32(Math.Round(StringToDouble(Col[0].ToString()) * 255, 0))),
                    Convert.ToByte(Col[1].ToString()),
                    Convert.ToByte(Col[2].ToString()),
                    Convert.ToByte(Col[3].ToString()));
            }
            catch (Exception)
            {
                return Color.FromArgb(255, 0, 0, 0);
            }
        }

Thank's a lot
0 Kudos
JenniferNery
Esri Regular Contributor
You can look at one of the marked answer in this SL forum: http://forums.silverlight.net/forums/p/144015/321286.aspx

I think this is the correct conversion from hexadecimal string to color.
0 Kudos