Select to view content in your preferred language

Getting started - coding query

574
2
10-20-2010 07:33 PM
DamianMilne
Emerging Contributor
Hi, this is my first attempt at silverlight so please forgive my ignorance.

I have created a very basic silverlight application to display one tiled mapservice (from arcgisonline) and one dynamic mapservice (from local server). This worked fine. Now I am attempting to add a sublayer list for my dynamic map service based on the code provided in ESRI's Silverlight API resource centre (http://help.arcgis.com/en/webapi/sil...m#SubLayerList). When I run the application, I get 2 error messages:

"The name 'InitializeComponent' does not exist in the current content"
"The name 'MyMap' does not exist in the current context"

Can anyone tell me what I'm doing wrong please? XAML and Code Behind below. Thanks, Damian

XAML:
<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot">
        <esri:Map x:Name="MyMap" Extent="125,0,145,-35">
            <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer" />
            <esri:ArcGISDynamicMapServiceLayer ID="FaunaMapLayer" Url="http://baa16920/ArcGIS/rest/services/FA_test_SL/MapServer" Initialized="ArcGISDynamicMapServiceLayer_Initialized" />
        </esri:Map>
        
        <Border Background="#99919191" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20,20,20,30" Padding="10" BorderBrush="Black">

            <Grid>

                <Grid.RowDefinitions>
                    <RowDefinition Height="15" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <TextBlock Text="Fauna Layers" Foreground="White" Grid.Row="0" >
                    <TextBlock.Effect>
                        <DropShadowEffect ShadowDepth="1" />
                    </TextBlock.Effect>
                </TextBlock>

                <ListBox Margin="0,5,0,0" ItemsSource="{Binding ElementName=MyMap, Path=Layers.[FaunaMapLayer].Layers}" Grid.Row="1">

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Margin="2" Name="FaunaMapLayer" Content="{Binding Name}" IsChecked="{Binding DefaultVisibility}" Tag="{Binding ID}" ClickMode="Press" Click="CheckBox_Click" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </Grid>
        </Border>

    </Grid>
</UserControl>


Code Behind:
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 System.Windows.Data;

namespace SilverlightApplication2
{
    public partial class SubLayerList : UserControl
    {
        public SubLayerList()
        {
            InitializeComponent();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox tickedCheckBox = sender as CheckBox;

            string serviceName = tickedCheckBox.Name;
            bool visible = (bool)tickedCheckBox.IsChecked;

            int layerIndex = (int)tickedCheckBox.Tag;

            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as
                ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;

            List<int> visibleLayerList =
                dynamicServiceLayer.VisibleLayers != null
                ? dynamicServiceLayer.VisibleLayers.ToList() : new List<int>();

            if (visible)
            {
                if (!visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Add(layerIndex);
            }
            else
            {
                if (visibleLayerList.Contains(layerIndex))
                    visibleLayerList.Remove(layerIndex);
            }

            dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray();
        }

        private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
        {
            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer =
                sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
            if (dynamicServiceLayer.VisibleLayers == null)
                dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
        }

        private int[] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService)
        {
            List<int> visibleLayerIDList = new List<int>();

            ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicService.Layers;

            for (int index = 0; index < layerInfoArray.Length; index++)
            {
                if (layerInfoArray[index].DefaultVisibility)
                    visibleLayerIDList.Add(index);
            }
            return visibleLayerIDList.ToArray();
        }
    }
}
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
Sure.

In your XAML, class definition is
x:Class="SilverlightApplication2.MainPage"

which means MainPage() constructor should exist.

But your code-behind does not match what your XAML expects.
public partial class SubLayerList : UserControl
    {
        public SubLayerList()
        {
...

If you replace SubLayerList in code-behind with MainPage, your code should work.

I think the problem is VisualStudio is not able to pair your XAML with a partial class definition, therefore fails to call InitializeComponent(). And in code-behind since you were accessing MyMap, VisualStudio tries to find where MyMap has been defined.
0 Kudos
DamianMilne
Emerging Contributor
Great, thanks Jennifer, worked a treat (as I'm sure you knew it would :D)
Cheers, Damian
0 Kudos