Ejemplo n.º 1
0
        private async void ApplyUpdates()
        {
            try
            {
                // Create default sync parameters.
                OfflineMapSyncParameters parameters = await _offlineMapSyncTask.CreateDefaultOfflineMapSyncParametersAsync();

                // Set the parameters to download all updates for the mobile map packages.
                parameters.PreplannedScheduledUpdatesOption = PreplannedScheduledUpdatesOption.DownloadAllUpdates;

                // Set the map package to rollback to the old state should the sync job fail.
                parameters.RollbackOnFailure = true;

                // Create a sync job using the parameters.
                OfflineMapSyncJob offlineMapSyncJob = _offlineMapSyncTask.SyncOfflineMap(parameters);

                // Get the results of the job.
                offlineMapSyncJob.Start();
                OfflineMapSyncResult result = await offlineMapSyncJob.GetResultAsync();

                // Check if the job succeeded.
                if (offlineMapSyncJob.Status == JobStatus.Succeeded)
                {
                    // Check if the map package needs to be re-opened.
                    if (result.IsMobileMapPackageReopenRequired)
                    {
                        // Re-open the mobile map package.
                        _mobileMapPackage.Close();
                        await _mobileMapPackage.LoadAsync();

                        // Check that the mobile map package was loaded.
                        if (_mobileMapPackage.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded && _mobileMapPackage.Maps.Any())
                        {
                            // Set the mapview to the map from the package.
                            Map offlineMap = _mobileMapPackage.Maps[0];
                            MyMapView.Map = offlineMap;

                            // Create an offline map sync task for the map.
                            _offlineMapSyncTask = await OfflineMapSyncTask.CreateAsync(offlineMap);
                        }
                        else
                        {
                            await Application.Current.MainPage.DisplayAlert("Error", "Failed to load the mobile map package.", "OK");
                        }
                    }

                    // Verify that the map is up to date and change the UI to reflect the update availability status.
                    CheckForScheduledUpdates();
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "Error syncing the offline map.", "OK");
                }
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }
        private async void ApplyUpdatesClicked(object sender, EventArgs e)
        {
            try
            {
                // Create default sync parameters.
                OfflineMapSyncParameters parameters = await _offlineMapSyncTask.CreateDefaultOfflineMapSyncParametersAsync();

                // Set the parameters to download all updates for the mobile map packages.
                parameters.PreplannedScheduledUpdatesOption = PreplannedScheduledUpdatesOption.DownloadAllUpdates;

                // Create a sync job using the parameters.
                OfflineMapSyncJob offlineMapSyncJob = _offlineMapSyncTask.SyncOfflineMap(parameters);

                // Get the results of the job.
                offlineMapSyncJob.Start();
                OfflineMapSyncResult result = await offlineMapSyncJob.GetResultAsync();

                // Check if the job succeeded.
                if (offlineMapSyncJob.Status == JobStatus.Succeeded)
                {
                    // Check if the map package needs to be re-opened.
                    if (result.IsMobileMapPackageReopenRequired)
                    {
                        // Re-open the mobile map package.
                        _mobileMapPackage.Close();
                        _mobileMapPackage = new MobileMapPackage(_mapPackagePath);
                        await _mobileMapPackage.LoadAsync();

                        // Check that the mobile map package was loaded.
                        if (_mobileMapPackage.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded && _mobileMapPackage.Maps.Any())
                        {
                            // Set the mapview to the map from the package.
                            Map offlineMap = _mobileMapPackage.Maps[0];
                            _myMapView.Map = offlineMap;

                            // Create an offline map sync task for the map.
                            _offlineMapSyncTask = await OfflineMapSyncTask.CreateAsync(offlineMap);
                        }
                        else
                        {
                            new UIAlertView("Error", "Failed to load the mobile map package.", (IUIAlertViewDelegate)null, "OK", null).Show();
                        }
                    }

                    // Verify that the map is up to date and change the UI to reflect the update availability status.
                    CheckForScheduledUpdates();
                }
                else
                {
                    new UIAlertView("Error", "Error syncing the offline map.", (IUIAlertViewDelegate)null, "OK", null).Show();
                }
            }
            catch (Exception ex)
            {
                new UIAlertView("Error", ex.Message, (IUIAlertViewDelegate)null, "OK", null).Show();
            }
        }
        /// <summary>
        /// Method that handles syncing the offline and online maps
        /// </summary>
        private async Task SyncMap(Map map)
        {
            var syncTask = await OfflineMapSyncTask.CreateAsync(map);

            // set parameters for sync
            var taskParams = new OfflineMapSyncParameters()
            {
                SyncDirection     = SyncDirection.Bidirectional,
                RollbackOnFailure = true
            };

            try
            {
                // set the job to perform the sync
                OfflineMapSyncJob = syncTask.SyncOfflineMap(taskParams);

                // update the progress property when progress changes
                OfflineMapSyncJob.ProgressChanged +=
                    (s, e) =>
                {
                    Progress = OfflineMapSyncJob.Progress;
                };

                // listen for job changed events
                OfflineMapSyncJob.JobChanged += (s, e) =>
                {
                    // if sync succeeds, raise success message
                    if (OfflineMapSyncJob.Status == JobStatus.Succeeded)
                    {
                        BroadcastMessenger.Instance.RaiseBroadcastMessengerValueChanged(true, Models.BroadcastMessageKey.SyncSucceeded);
                    }
                    else if (OfflineMapSyncJob.Status == JobStatus.Failed)
                    {
                        // if the job failed display the error, unless the user has cancelled it on purpose
                        if (OfflineMapSyncJob.Error != null && OfflineMapSyncJob.Error.Message != "User canceled: Job canceled.")
                        {
                            UserPromptMessenger.Instance.RaiseMessageValueChanged(
                                null, OfflineMapSyncJob.Error.Message, true, OfflineMapSyncJob.Error.StackTrace);
                        }

                        BroadcastMessenger.Instance.RaiseBroadcastMessengerValueChanged(false, Models.BroadcastMessageKey.SyncSucceeded);
                    }
                };

                // begin sync job
                OfflineMapSyncJob.Start();
            }
            catch (Exception ex)
            {
                UserPromptMessenger.Instance.RaiseMessageValueChanged(
                    null, ex.Message, true, ex.StackTrace);

                BroadcastMessenger.Instance.RaiseBroadcastMessengerValueChanged(false, Models.BroadcastMessageKey.SyncSucceeded);
            }
        }