Connects the Layer to an underlying data source.
Namespace:
ESRI.ArcGISExplorer.Mapping
Assembly:
ESRI.ArcGISExplorer (in ESRI.ArcGISExplorer.dll) Version: 2.0.0.1500 (2.0.0.1500)
Syntax
C# |
---|
public virtual bool Connect() |
Visual Basic (Declaration) |
---|
Public Overridable Function Connect As Boolean |
Return Value
trueTruetruetrue (True in Visual Basic) if the Layer was connected successfully; otherwise
falseFalsefalsefalse (False in Visual Basic)Remarks
Examples
The code below illustrates how to populate the data source properties for each derived Layer type and attempts to connect to
the underlying data source. If the connection is successful, the Layer is added to a map. If unsuccessful, the connection error
is logged.
CopyC#
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;
using System.Drawing;
using ESRI.ArcGISExplorer.Data;
using ESRI.ArcGISExplorer.Mapping;
namespace ExamplesCS.Mapping
{
class LayerConnectionExample
{
private void CheckConnectionStatus(Layer layer)
{
ConnectionStatus status = layer.ConnectionStatus;
if (status.HasError)
{
System.Diagnostics.Debug.Print("Error message: " + status.ErrorString);
if(status.ErrorException != null)
System.Diagnostics.Debug.Print("Call stack: " + status.ErrorException.StackTrace);
}
}
public void Layer_Connections()
{
Map map = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map;
DataSourceProperties featureLayerDataSourceProps = new DataSourceProperties(@"C:\Data\Forestry.gdb", "road_hazards");
FeatureLayer featureLayer = new FeatureLayer(featureLayerDataSourceProps);
bool isConnected = featureLayer.Connect();
if (isConnected)
map.ChildItems.Add(featureLayer);
else
CheckConnectionStatus(featureLayer);
DataSourceProperties rasterLayerDataSourceProps = new DataSourceProperties(@"C:\Data\Scotland.gdb", "NORTHGORMS_HO");
RasterLayer rasterLayer = new RasterLayer(rasterLayerDataSourceProps);
isConnected = rasterLayer.Connect();
if (isConnected)
map.ChildItems.Add(rasterLayer);
else
CheckConnectionStatus(rasterLayer);
Uri serverUri = new Uri("http://services.arcgisonline.com/Server/Services");
string serviceName = "ESRI_Imagery_World_2D";
ServiceConnectionProperties serviceLayerConnectionProps = new ServiceConnectionProperties(ServiceType.MapServer, serverUri, serviceName);
ServiceLayer serviceLayer = new ServiceLayer(serviceLayerConnectionProps);
isConnected = serviceLayer.Connect();
if (isConnected)
map.ChildItems.Add(serviceLayer);
else
CheckConnectionStatus(serviceLayer);
Uri geoRssUri = new Uri("http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/6697.xml");
GeoRssLayer geoRssLayer = new GeoRssLayer(geoRssUri);
isConnected = geoRssLayer.Connect();
if (isConnected)
map.ChildItems.Add(geoRssLayer);
else
CheckConnectionStatus(geoRssLayer);
string pathToKmlFile = @"C:\Data\KmlExample.kml";
KmlLayer kmlLayer = new KmlLayer(pathToKmlFile);
isConnected = kmlLayer.Connect();
if (isConnected)
map.ChildItems.Add(kmlLayer);
else
CheckConnectionStatus(kmlLayer);
string pathToPackageLayer = @"C:\Data\LpkExample.lpk";
PackageLayer packageLayer = new PackageLayer(pathToPackageLayer);
isConnected = packageLayer.Connect();
if (isConnected)
map.ChildItems.Add(packageLayer);
else
CheckConnectionStatus(packageLayer);
}
}
}
CopyVB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.Windows.Forms
Imports System.Drawing
Imports ESRI.ArcGISExplorer.Data
Imports ESRI.ArcGISExplorer.Mapping
Class LayerConnectionExample
Private Sub CheckConnectionStatus(ByVal layer As Layer)
Dim status As ConnectionStatus = layer.ConnectionStatus
If status.HasError Then
System.Diagnostics.Debug.Print("Error message: " + status.ErrorString)
If status.ErrorException IsNot Nothing Then
System.Diagnostics.Debug.Print("Call stack: " + status.ErrorException.StackTrace)
End If
End If
End Sub
Public Sub Layer_Connections()
Dim map As Map = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map
Dim featureLayerDataSourceProps As New DataSourceProperties("C:\Data\Forestry.gdb", "road_hazards")
Dim featureLayer As New FeatureLayer(featureLayerDataSourceProps)
Dim isConnected As Boolean = featureLayer.Connect()
If isConnected Then
map.ChildItems.Add(featureLayer)
Else
CheckConnectionStatus(featureLayer)
End If
Dim rasterLayerDataSourceProps As New DataSourceProperties("C:\Data\Scotland.gdb", "NORTHGORMS_HO")
Dim rasterLayer As New RasterLayer(rasterLayerDataSourceProps)
isConnected = rasterLayer.Connect()
If isConnected Then
map.ChildItems.Add(rasterLayer)
Else
CheckConnectionStatus(rasterLayer)
End If
Dim serverUri As New Uri("http://services.arcgisonline.com/Server/Services")
Dim serviceName As String = "ESRI_Imagery_World_2D"
Dim serviceLayerConnectionProps As New ServiceConnectionProperties(ServiceType.MapServer, serverUri, serviceName)
Dim serviceLayer As New ServiceLayer(serviceLayerConnectionProps)
isConnected = serviceLayer.Connect()
If isConnected Then
map.ChildItems.Add(serviceLayer)
Else
CheckConnectionStatus(serviceLayer)
End If
Dim geoRssUri As New Uri("http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/6697.xml")
Dim geoRssLayer As New GeoRssLayer(geoRssUri)
isConnected = geoRssLayer.Connect()
If isConnected Then
map.ChildItems.Add(geoRssLayer)
Else
CheckConnectionStatus(geoRssLayer)
End If
Dim pathToKmlFile As String = "C:\Data\KmlExample.kml"
Dim kmlLayer As New KmlLayer(pathToKmlFile)
isConnected = kmlLayer.Connect()
If isConnected Then
map.ChildItems.Add(kmlLayer)
Else
CheckConnectionStatus(kmlLayer)
End If
Dim pathToPackageLayer As String = "C:\Data\LpkExample.lpk"
Dim packageLayer As New PackageLayer(pathToPackageLayer)
isConnected = packageLayer.Connect()
If isConnected Then
map.ChildItems.Add(packageLayer)
Else
CheckConnectionStatus(packageLayer)
End If
End Sub
End Class
See Also