Hi RobertYes, It is possible create a new feature in a LocalfeatureLayer with selected geometry. One way to do this is to get the geometry you want from a map control mouse click event and create a new graphic with this geometry set. The new graphic can then be saved as an edit to the LocalFeatureLayer. In the scenario where you cannot enter null fields into a database it is possible to get prototype attributes from the current features in the LocalFeatureLayer and set default values where appropriate. There is example code below:The following code will start the LocalFeatureService asynchronously with a path to the Map Package and the ArcGISLocalFeatureLayer will use this service. ## Default constructor ##
public partial class MainWindow : Window
{
ArcGISLocalFeatureLayer arcGISLocalFeatureLayer;
ESRI.ArcGIS.Client.Geometry.MapPoint _clickPoint;
public MainWindow()
{
InitializeComponent();
LocalFeatureService.GetServiceAsync("Path to .mpk", (localFeatureService) =>
{
arcGISLocalFeatureLayer = new ArcGISLocalFeatureLayer()
{
Service = localFeatureService,
Editable = true,
LayerId = 0,
DisableClientCaching = true,
AutoSave = false,
Mode = ESRI.ArcGIS.Client.FeatureLayer.QueryMode.OnDemand,
OutFields = new OutFields() { "*" },
};
The following code contains the Initialized, InitializationFailed and UpdateCompleted event handlers. Normally, if you add a LocalFeatureLayer to the map, the initialized event fires automatically once the map has initialized the layer, and then an updatecompleted event fires as the LocalFeaturelayer makes an Update call immediately after initialization in snapshot or ondemand modes. Once UpdateCompleted has fired, the feature layer graphics collection will be populated with features. In this example the Mapcontrol is made enabled when the updatecompleted event fires. At each stage it is possible to count how many features there are in the LocalFeatureLayer.
arcGISLocalFeatureLayer.Initialized += (s, e) =>
{
MessageBox.Show(arcGISLocalFeatureLayer.Graphics.Count.ToString());
};
arcGISLocalFeatureLayer.InitializationFailed += (s, e) =>
{
MessageBox.Show(arcGISLocalFeatureLayer.InitializationFailure.Message);
};
arcGISLocalFeatureLayer.UpdateCompleted += (s, e) =>
{
_mapControl.IsEnabled = true;
MessageBox.Show(arcGISLocalFeatureLayer.Graphics.Count.ToString());
};
The following code adds the LocalFeatureLayer to the map control. You don't need to add the LocalFeatureLayer to your map control to perform edits, you just need to make sure it is initialized and updated. Normally the map is responsible for the layer initialization and initial update method call so if you don�??t add the feature layer to the map you�??ll need to call Initialize() explicitly, and respond to the Initialized event in which you would call Update().
_mapControl.Layers.Insert(1,arcGISLocalFeatureLayer);
## Mapcontrol MouseClick event##In the following code the _mapControl_MouseClick method is a handler for the Map's MouseClick event. The user clicks on the map to specify a point geometry which is then used to create a new graphic, which is added to the LocalFeatureLayer and saved.
private void _mapControl_MouseClick(object sender, Map.MouseEventArgs e)
{
_clickPoint = e.MapPoint;
var graphic = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = _clickPoint,
};
The following code gets the prototype attributes from the features in the LocalFeatureLayer and sets default values as appropriate. This prevents issues where fields in the database cannot be null.
if (arcGISLocalFeatureLayer.LayerInfo == null) return;
//use LayerInfo.Fields
foreach (var field in arcGISLocalFeatureLayer.LayerInfo.Fields)
graphic.Attributes[field.Name] = null;
//use PrototypeAttributes
FeatureTemplate featureTemplate = null;
if (arcGISLocalFeatureLayer.LayerInfo.Templates != null && arcGISLocalFeatureLayer.LayerInfo.Templates.Count > 0)
featureTemplate = arcGISLocalFeatureLayer.LayerInfo.Templates.FirstOrDefault().Value;
else if (arcGISLocalFeatureLayer.LayerInfo.FeatureTypes != null && arcGISLocalFeatureLayer.LayerInfo.FeatureTypes.Count > 0)
{
var featureType = arcGISLocalFeatureLayer.LayerInfo.FeatureTypes.FirstOrDefault();
if (featureType.Value != null && featureType.Value.Templates != null)
featureTemplate = featureType.Value.Templates.FirstOrDefault().Value;
}
if (featureTemplate != null && featureTemplate.PrototypeAttributes != null)
{
foreach (var item in featureTemplate.PrototypeAttributes)
graphic.Attributes[item.Key] = item.Value;
}
The following code adds the new graphic to your LocalFeatureLayer. The graphic contains all the default attributes.
arcGISLocalFeatureLayer.Graphics.Add(graphic);
The following code includes the BeginSaveEdits, EndSaveEdits and SaveEditsFailed events where you can check the graphics count has increased on EndSaveEdits and catch any failures saving the edits.
arcGISLocalFeatureLayer.BeginSaveEdits += (s1, beginEditEventArgs) =>
{
};
arcGISLocalFeatureLayer.EndSaveEdits += (s2, endEditEventArgs) =>
{
MessageBox.Show(arcGISLocalFeatureLayer.Graphics.Count.ToString());
};
arcGISLocalFeatureLayer.SaveEditsFailed += (s3, taskFailedEventArgs) =>
{
string message = taskFailedEventArgs.Error.Message;
ServiceException serviceEx = taskFailedEventArgs.Error as ServiceException;
if (serviceEx != null && serviceEx.Details != null && serviceEx.Details.Count > 0)
message += serviceEx.Details[0];
};
The following code saves the edits to the LocalFeatureLayer. If the LocalFeatureLayer was setup with AutoSave = true then the edit will be automatically be saved back to the Geodatabase via the [local]service. It�??s only necessary to call SaveEdits explicitly if AutoSave is false.
arcGISLocalFeatureLayer.SaveEdits();
} } }
CheersKerrie