Display an offline map (custom)

Learn how to download and display an offline map for a user-defined geographical area of a web map.

display an offline map custom

Offline maps allow users to continue working when network connectivity is poor or lost. If a web map is enabled for offline use, a user can request that ArcGIS generates an offline map for a specified geographic area of interest.

In this tutorial, you will download an offline map for an area of interest from the web map of the stormwater network within Naperville, IL, USA. You can then use this offline map without a network connection.

To learn how to enable your web map for offline use, see the Offline enable a web map tutorial.

Before starting this tutorial, you should:

  1. Have an ArcGIS account and an API key to access ArcGIS services. If you don't have an account, sign up for free.
  2. Ensure your development environment meets the system requirements.

Optionally, you may want to install the ArcGIS Runtime SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.

Steps

Open a Visual Studio solution

  1. To start the tutorial, complete the Display a web map tutorial or download and unzip the solution.

  2. Open the .sln file in Visual Studio.

  3. If you downloaded the solution project, set your API key.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    If necessary, set the API Key.
    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In Visual Studio, in the Solution Explorer, click App.xaml.cs.

    3. In the App class, add an override for the OnStartup() function to set the ApiKey property on ArcGISRuntimeEnvironment.

      App.xaml.cs
      Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                              
      //   Copyright 2021 Esri
      //   Licensed under the Apache License, Version 2.0 (the "License");
      //   you may not use this file except in compliance with the License.
      //   You may obtain a copy of the License at
      //
      //   https://www.apache.org/licenses/LICENSE-2.0
      //
      //   Unless required by applicable law or agreed to in writing, software
      //   distributed under the License is distributed on an "AS IS" BASIS,
      //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      //   See the License for the specific language governing permissions and
      //   limitations under the License.
      
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Threading.Tasks;
      using System.Windows;
      
      namespace DisplayAMap
      {
          /// <summary>
          /// Interaction logic for App.xaml
          /// </summary>
      
          public partial class App : Application
          {
      
              protected override void OnStartup(StartupEventArgs e)
              {
                  base.OnStartup(e);
                  // Note: it is not best practice to store API keys in source code.
                  // The API key is referenced here for the convenience of this tutorial.
                  Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_API_KEY";
              }
      
          }
      }
      

If developing with Visual Studio for Windows, ArcGIS Runtime for .NET provides a set of project templates for each of the supported .NET platforms. These templates provide all of the code needed for a basic Model-View-ViewModel (MVVM) app. You need to install the ArcGIS Runtime for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.

Update the tutorial name used in the project (optional)

The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAWebMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.

The tutorial instructions and code use the name DisplayAnOfflineMapCustom for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.

  1. Update the name for the solution and the project.

    • In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
    • In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
  2. Rename the namespace used by classes in the project.

    • In the Solution Explorer, expand the project node.
    • Double-click MapViewModel.cs in the Solution Explorer to open the file.
    • In the MapViewModel class, double-click the namespace name (DisplayAWebMap) to select it, and then right-click and choose Rename....
    • Provide the new name for the namespace.
    • Click Apply in the Rename: DisplayAWebMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
  3. Build the project.

    • Choose Build > Build solution (or press <F6>).

Get the web map item ID

You can use ArcGIS tools to create and view web maps. Use the Map Viewer to identify the web map item ID. This item ID will be used later in the tutorial.

  1. Go to the Naperville water network in the Map Viewer in ArcGIS Online. This web map displays stormwater network within Naperville, IL, USA.
  2. Make a note of the item ID at the end of the browser's URL. The item ID should be acc027394bc84c2fb04d1ed317aac674

Display the web map

You can display a web map using the web map's item ID. Create a map from the web map portal item, and display it in your app.

  1. In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.

    The project uses the Model-View-ViewModel (MVVM) design pattern to separate the application logic (view model) from the user interface (view). MapViewModel.cs contains the view model class for the application, called MapViewModel. See the Microsoft documentation for more information about the Model-View-ViewModel pattern.

  2. Add additional required using statements at the top of the class.

    MapViewModel.cs
    Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  3. In MapViewModel.cs, modify the SetupMap() function to update the web map's item ID to that of the Naperville water network.

    The existing code creates a PortalItem using the item ID and an ArcGISPortal referencing ArcGIS Online. It sets the MapViewModel.Map property to a new Map created using the PortalItem.

    MapViewModel.cs
    Change line
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  4. Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.

You should see a map of the stormwater network within Naperville, IL, USA. Use the mouse to drag, scroll, and double-click the map view to explore the map.

Specify an area of the web map to take offline

You can specify an area of the web map to take offline using either an Envelope or a Polygon. You can use a graphic to display the area on the map.

  1. In the MapViewModel class, create a new property named GraphicsOverlays. This will be a collection of GraphicsOverlay to display a graphic of the area to take offline.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  2. Modify the SetupMap() function. Use an EnvelopeBuilder to create a new Envelope that defines an area to take offline.

    MapViewModel.cs
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  3. Display a graphic of the area to take offline.

    Use a SimpleLineSymbol and a SimpleFillSymbol to display a new Graphic of the offlineArea with a red outline. Add the graphic to a new GraphicsOverlay and set the MapViewModel.GraphicsOverlays property to display it in the map view.

    MapViewModel.cs
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  4. In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.

  5. Use data binding to bind the GraphicsOverlays property of the MapViewModel to the MapView control.

    Data binding and the Model-View-ViewModel (MVVM) design pattern allow you to separate the logic in your app (the view model) from the presentation layer (the view).

    Add line.
                             
    
    <Window x:Class="DisplayAnOfflineMapCustom.MainWindow"
            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:local="clr-namespace:DisplayAnOfflineMapCustom"
            xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
    
        <Window.Resources>
            <local:MapViewModel x:Key="MapViewModel" />
        </Window.Resources>
    
        <Grid>
    
            <esri:MapView x:Name="MainMapView"
                              Map="{Binding Map, Source={StaticResource MapViewModel}}"
                              GraphicsOverlays="{Binding GraphicsOverlays, Source={StaticResource MapViewModel}}" />
    
    
        </Grid>
    
    </Window>
    
  6. Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.

You should see a red outline on the stormwater network within Naperville, IL, USA. This indicates the area of the web map that you are going to take offline.

Download and display the offline map

You can generate and download an offline map for a specified area of interest using an asynchronous task. When complete, it will provide the offline map for display in your map view.

  1. Return to the MapViewModel.cs file and add code to the SetupMap function to create an OfflineMapTask that references the online map by calling the static OfflineMapTask.CreateAsync method.

    MapViewModel.cs
    Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  2. Get default parameters to generate and download the offline map. Modify them to download a read-only offline map.

    This tutorial does not involve editing and updating the contents of the offline map. When an offline map is editable, metadata is stored in ArcGIS to track and synchronize edits. Setting the GenerateOfflineMapUpdateMode to NoUpdates avoids the overhead of maintaining this metadata in ArcGIS.

    MapViewModel.cs
    Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  3. Set a download location for the offline map.

    MapViewModel.cs
    Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    

    This tutorial code creates a new unique folder in the documents folder using the current date and time.

  4. Create a new GenerateOfflineMapJob using the parameters and downloadDirectory.

    MapViewModel.cs
    Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  5. Create a function called GenerateJob_ProgressChanged. This function will respond when the job completes or fails and will track the precent complete as the job runs. Add a try/catch block to handle exceptions and start by getting a reference to the GenerateOfflineMapJob, passed in as the sender argument.

    MapViewModel.cs
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  6. If the job succeeds, set the MapViewModel.Map property with the offline map result. If it fails, notify the user. If the job is running, write the percent complete to the console.

    MapViewModel.cs
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  7. Return to your code in SetupMap and assign the GenerateJob_ProgressChanged function to handle the Job.ProgressChanged event.

    MapViewModel.cs
    Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  8. Start the generateJob by calling its Start() method.

    MapViewModel.cs
    Add line.
                                                                                                                                                               
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Portal;
    
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Offline;
    using Esri.ArcGISRuntime.UI;
    using System.Windows;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace DisplayAnOfflineMapCustom
    {
        class MapViewModel : INotifyPropertyChanged
        {
            public MapViewModel()
            {
                SetupMap();
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private Map _map;
            public Map Map
            {
                get { return _map; }
                set
                {
                    _map = value;
                    OnPropertyChanged();
                }
            }
    
            private GraphicsOverlayCollection _graphicsOverlays;
            public GraphicsOverlayCollection GraphicsOverlays
            {
                get { return _graphicsOverlays; }
                set
                {
                    _graphicsOverlays = value;
                    OnPropertyChanged();
                }
            }
    
            private async Task SetupMap()
            {
                // Create a portal pointing to ArcGIS Online.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    
                // Create a portal item for a specific web map id.
    
                string webMapId = "acc027394bc84c2fb04d1ed317aac674";
    
                PortalItem mapItem = await PortalItem.CreateAsync(portal, webMapId);
    
                // Create the map from the item.
                Map map = new Map(mapItem);
    
                // Set the view model "Map" property.
                this.Map = map;
    
                // Define area of interest (envelope) to take offline.
                EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(SpatialReferences.Wgs84)
                {
                    XMin = -88.1526,
                    XMax = -88.1490,
                    YMin = 41.7694,
                    YMax = 41.7714
                };
    
                Envelope offlineArea = envelopeBldr.ToGeometry();
    
                // Create a graphic to display the area to take offline.
                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Red, 2);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
                Graphic offlineAreaGraphic = new Graphic(offlineArea, fillSymbol);
    
                // Create a graphics overlay and add the graphic.
                GraphicsOverlay areaOverlay = new GraphicsOverlay();
                areaOverlay.Graphics.Add(offlineAreaGraphic);
    
                // Add the overlay to a new graphics overlay collection.
                GraphicsOverlayCollection overlays = new GraphicsOverlayCollection
                {
                    areaOverlay
                };
    
                // Set the view model's "GraphicsOverlays" property (will be consumed by the map view).
                this.GraphicsOverlays = overlays;
    
                // Create an offline map task using the current map.
                OfflineMapTask offlineMapTask = await OfflineMapTask.CreateAsync(map);
    
                // Create a default set of parameters for generating the offline map from the area of interest.
                GenerateOfflineMapParameters parameters = await offlineMapTask.CreateDefaultGenerateOfflineMapParametersAsync(offlineArea);
                parameters.UpdateMode = GenerateOfflineMapUpdateMode.NoUpdates;
    
                // Build a folder path named with today's date/time in the "My Documents" folder.
                string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string downloadDirectory = System.IO.Path.Combine(documentsFolder, "OfflineMap_" + DateTime.Now.ToFileTime().ToString());
    
                GenerateOfflineMapJob generateJob = offlineMapTask.GenerateOfflineMap(parameters, downloadDirectory);
    
                generateJob.ProgressChanged += GenerateJob_ProgressChanged;
    
                generateJob.Start();
    
            }
    
            private async void GenerateJob_ProgressChanged(object sender, EventArgs e)
            {
    
                try
                {
                    GenerateOfflineMapJob generateJob = (GenerateOfflineMapJob)sender;
    
                    // If the job succeeds, show the offline map in the map view.
                    if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
                    {
                        var result = await generateJob.GetResultAsync();
                        this.Map = result.OfflineMap;
                        Debug.WriteLine("Generate offline map: Complete");
                    }
                    // If the job fails, notify the user.
                    else if (generateJob.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
                    {
                        MessageBox.Show($"Unable to generate a map for that area: {generateJob.Error.Message}");
                    }
                    else
                    {
                        int percentComplete = generateJob.Progress;
                        Debug.WriteLine($"Generate offline map: {percentComplete}%");
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error generating offline map: {ex.Message}");
                }
    
            }
    
        }
    }
    
  9. Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.

You should see an offline map for the specified area of the stormwater network within Naperville, IL, USA. Remove your network connection and you will still be able to use the mouse to drag, scroll, and double-click the map view to explore this offline map.

What's next?

Learn how to use additional API features, ArcGIS platform services, and ArcGIS tools in these tutorials: