This sample demonstrates using the ESRI.ArcGIS.Client.Toolkit AttachmentEditor to upload, download, and delete files associated with selected features in a FeatureLayer.
To use the sample, select a point feature and click the 'Add' button to attach a file. To download an attachment for a selected feature, click the hyperlink for the filename. To delete an attachment associated with a selected feature click the red X next to the file name.
<UserControlx:Class="ArcGISWPFSDK.ToolkitAttachmentEditor"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:esri="http://schemas.esri.com/arcgis/client/2009"><Gridx:Name="LayoutRoot"Background="White"><Grid.Resources><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"/><GradientStopColor="#FF3D7FAC"Offset="0.184"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"Loaded="MyMap_Loaded"WrapAround="True"><esri:ArcGISTiledMapServiceLayerUrl="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:FeatureLayerID="lyrIncidents"AutoSave="False"Initialized="FeatureLayer_Initialized"DisableClientCaching="True"OutFields="*"Mode="OnDemand"Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"></esri:FeatureLayer></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,15,15,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><StackPanelx:Name="myStack"Orientation="Vertical"><TextBlockText="Click on a point feature to select it, and click the 'Add' button to attach a file."Width="275"TextAlignment="Left"Margin="20,20,20,5"TextWrapping="Wrap"FontWeight="Bold"Foreground="Black"/><esri:AttachmentEditorx:Name="attachmentEditor"VerticalAlignment="Top"Margin="20,5,20,20"Background="WhiteSmoke"Width="280"Height="190"HorizontalAlignment="Right"Filter="All Files (*.*)|*.*|Image Files|*.tif;*.jpg;*.gif;*.png;*.bmp|Text Files (.txt)|*.txt"FilterIndex="1"Multiselect="True"UploadFailed="attachmentEditor_UploadFailed"></esri:AttachmentEditor></StackPanel></Grid></Grid></UserControl>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
namespace ArcGISWPFSDK
{
publicpartialclass ToolkitAttachmentEditor : UserControl
{
private FeatureLayer featureLayer;
privatestatic ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
public ToolkitAttachmentEditor()
{
InitializeComponent();
featureLayer = MyMap.Layers[1] as FeatureLayer;
ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
new ESRI.ArcGIS.Client.Geometry.Envelope(
_mercator.FromGeographic(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-122.4306073721, 37.7666097907)) as MapPoint,
_mercator.FromGeographic(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-122.4230971868, 37.77197420877)) as MapPoint);
initialExtent.SpatialReference = new SpatialReference(102100);
MyMap.Extent = initialExtent;
}
privatevoid FeatureLayer_Initialized(object sender, EventArgs e)
{
if (featureLayer != null)
featureLayer.MouseLeftButtonUp += FeatureLayer_MouseLeftButtonUp;
}
privatevoid FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
{
for (int i = 0; i < featureLayer.SelectionCount; i++)
featureLayer.SelectedGraphics.ToList()[i].UnSelect();
e.Graphic.Select();
attachmentEditor.GraphicSource = e.Graphic;
}
privatevoid attachmentEditor_UploadFailed(object sender, ESRI.ArcGIS.Client.Toolkit.AttachmentEditor.UploadFailedEventArgs e)
{
MessageBox.Show(e.Result.Message);
}
privatevoid MyMap_Loaded(object sender, RoutedEventArgs e)
{
attachmentEditor.FeatureLayer = featureLayer;
}
}
}
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Namespace ArcGISWPFSDK
PartialPublicClass ToolkitAttachmentEditor
Inherits UserControl
Private featureLayer As FeatureLayer
PrivateShared _mercator AsNew ESRI.ArcGIS.Client.Projection.WebMercator()
PublicSubNew()
InitializeComponent()
featureLayer = TryCast(MyMap.Layers(1), FeatureLayer)
Dim initialExtent AsNew ESRI.ArcGIS.Client.Geometry.Envelope(TryCast(_mercator.FromGeographic(New ESRI.ArcGIS.Client.Geometry.MapPoint(-122.4306073721, 37.7666097907)), MapPoint), TryCast(_mercator.FromGeographic(New ESRI.ArcGIS.Client.Geometry.MapPoint(-122.4230971868, 37.77197420877)), MapPoint))
initialExtent.SpatialReference = New SpatialReference(102100)
MyMap.Extent = initialExtent
EndSubPrivateSub FeatureLayer_Initialized(sender AsObject, e As EventArgs)
If featureLayer IsNotNothingThenAddHandler featureLayer.MouseLeftButtonUp, AddressOf FeatureLayer_MouseLeftButtonUp
EndIfEndSubPrivateSub FeatureLayer_MouseLeftButtonUp(sender AsObject, e As GraphicMouseButtonEventArgs)
For i AsInteger = 0 To featureLayer.SelectionCount - 1
featureLayer.SelectedGraphics.ToList()(i).UnSelect()
Next
e.Graphic.[Select]()
attachmentEditor.GraphicSource = e.Graphic
EndSubPrivateSub attachmentEditor_UploadFailed(sender AsObject, e As ESRI.ArcGIS.Client.Toolkit.AttachmentEditor.UploadFailedEventArgs)
MessageBox.Show(e.Result.Message)
EndSubPrivateSub MyMap_Loaded(sender AsObject, e As RoutedEventArgs)
attachmentEditor.FeatureLayer = featureLayer
EndSubEndClassEndNamespace