private async void OnSaveMapClick(object sender, RoutedEventArgs e)
{
try
{
// Create a challenge request for portal credentials (OAuth credential request for arcgis.com)
CredentialRequestInfo challengeRequest = new CredentialRequestInfo();
// Use the OAuth implicit grant flow
challengeRequest.GenerateTokenOptions = new GenerateTokenOptions
{
TokenAuthenticationType = TokenAuthenticationType.OAuthImplicit
};
// Indicate the url (portal) to authenticate with (ArcGIS Online)
challengeRequest.ServiceUri = new Uri("https://www.arcgis.com/sharing/rest");
// Call GetCredentialAsync on the AuthenticationManager to invoke the challenge handler
await AuthenticationManager.Current.GetCredentialAsync(challengeRequest, false);
// Get information for the new portal item
var title = TitleTextBox.Text;
var description = DescriptionTextBox.Text;
var tags = TagsTextBox.Text.Split(',');
// Throw an exception if the text is null or empty for title or description
if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(description))
{
throw new Exception("Title and description are required");
}
// Get current map extent (viewpoint) for the map initial extent
var currentViewpoint = MyMapView.GetCurrentViewpoint(Esri.ArcGISRuntime.Mapping.ViewpointType.BoundingGeometry);
// Export the current map view to use as the item's thumbnail
RuntimeImage thumbnailImg = await MyMapView.ExportImageAsync();
// See if the map has already been saved
if (!_mapViewModel.MapIsSaved)
{
// Call the SaveNewMapAsync method on the view model, pass in the required info
await _mapViewModel.SaveNewMapAsync(currentViewpoint, title, description, tags, thumbnailImg);
// Report success
MessageBox.Show("Map '" + title + "' was saved to your portal");
}
else
{
// Map has previously been saved as a portal item, update it (title, description, and tags will remain the same)
_mapViewModel.UpdateMapItem();
// Report success
MessageBox.Show("Changes to '" + title + "' were updated to the portal.");
}
}
catch (Exception ex)
{
// Report error
MessageBox.Show("Error while saving: " + ex.Message);
}
}