Explicit Save

Download Samples Repository

Description

Demonstrates how to accumulated edits can be saved or canceled altogether.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ExplicitSave"
             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/runtime/2013">
    <Grid>
        <esri:MapView x:Name="MyMapView">            
            <esri:Map InitialViewpoint="-13045660.491307795,4036200.4792818795,-13044437.4988552,4037423.471734474,102100">
                <esri:ArcGISTiledMapServiceLayer ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                <!-- Makes all features at current extent available for editing -->
                <esri:FeatureLayer ID="Notes">
                    <esri:ServiceFeatureTable ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Notes/FeatureServer/0"/>
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>
        <!-- Used for accumulating, saving or canceling edits -->
        <Border Background="White"
                BorderBrush="Black"
                BorderThickness="1"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="30"
                Padding="20">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <StackPanel Orientation="Horizontal">
                <Button Content="Add"
                        Margin="2"
                        Click="AddButton_Click" />
                <Button Content="Save"
                        Margin="2"
                        Click="SaveButton_Click" />
                <Button Content="Cancel"
                        Margin="2"
                        Click="CancelButton_Click" />
            </StackPanel>
        </Border>
        <Border Background="White"
                BorderBrush="Black"
                BorderThickness="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Margin="30"
                Padding="20">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <TextBlock Text="'Add' new feature and then 'save' or 'cancel' edits when ready."
                       Width="200"
                       TextAlignment="Left"
                       Margin="30,20,20,30"
                       TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates how to accumulated edits can be saved or canceled altogether.
    /// </summary>
    /// <title>Explicit Save</title>
    /// <category>Editing</category>
    public partial class ExplicitSave : UserControl
    {
        public ExplicitSave()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Adds new feature on tap.
        /// </summary>
        private async void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var layer = MyMapView.Map.Layers["Notes"] as FeatureLayer;
            var table = layer.FeatureTable;
            string message = null;
            try
            {
                var mapPoint = await MyMapView.Editor.RequestPointAsync();
                var feature = new GeodatabaseFeature(table.Schema)
                {
                    Geometry = mapPoint
                };
                await table.AddAsync(feature);
            }
            catch (TaskCanceledException te)
            {
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            if (!string.IsNullOrWhiteSpace(message))
                MessageBox.Show(message);
        }

        /// <summary>
        /// Saves accumulated edits.
        /// </summary>
        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            var layer = MyMapView.Map.Layers["Notes"] as FeatureLayer;
            var table = (ArcGISFeatureTable)layer.FeatureTable;            
            string message = null;
            try
            {
                if (!table.HasEdits)
                    return;
                if (table is ServiceFeatureTable)
                {
                    var serviceTable = (ServiceFeatureTable)table;
                    // Pushes accumulated edits back to the server.
                    var saveResult = await serviceTable.ApplyEditsAsync();
                    if (saveResult != null && saveResult.AddResults != null
                        && saveResult.AddResults.All(r => r.Error == null && r.Success))
                        message = string.Format("Saved {0} features", saveResult.AddResults.Count);
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            if (!string.IsNullOrWhiteSpace(message))
                MessageBox.Show(message);
        }

        /// <summary>
        /// Cancels accumulated edits.
        /// </summary>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            var layer = MyMapView.Map.Layers["Notes"] as FeatureLayer;
            var table = (ArcGISFeatureTable)layer.FeatureTable;
            if (table.HasEdits)
                table.ClearEdits();
        }
    }
}
Feedback on this topic?