|
| 1 | +// /******************************************************************************* |
| 2 | +// * Copyright 2012-2018 Esri |
| 3 | +// * |
| 4 | +// * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// * you may not use this file except in compliance with the License. |
| 6 | +// * You may obtain a copy of the License at |
| 7 | +// * |
| 8 | +// * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// * |
| 10 | +// * Unless required by applicable law or agreed to in writing, software |
| 11 | +// * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// * See the License for the specific language governing permissions and |
| 14 | +// * limitations under the License. |
| 15 | +// ******************************************************************************/ |
| 16 | + |
| 17 | +using Esri.ArcGISRuntime.Mapping.Popups; |
| 18 | +using Microsoft.Win32; |
| 19 | +using System.Windows.Controls.Primitives; |
| 20 | + |
| 21 | +namespace Esri.ArcGISRuntime.Toolkit.Primitives |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Supporting control for the <see cref="Esri.ArcGISRuntime.Toolkit.UI.Controls.PopupViewer"/> control, |
| 25 | + /// used for rendering a <see cref="AttachmentsPopupElement"/>. |
| 26 | + /// </summary> |
| 27 | + [TemplatePart(Name ="AttachmentList", Type = typeof(ListBox))] |
| 28 | + public class AttachmentsPopupElementView : Control |
| 29 | + { |
| 30 | + private ListBox? itemsList; |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="AttachmentsPopupElementView"/> class. |
| 33 | + /// </summary> |
| 34 | + public AttachmentsPopupElementView() |
| 35 | + { |
| 36 | + DefaultStyleKey = typeof(AttachmentsPopupElementView); |
| 37 | + } |
| 38 | + |
| 39 | + /// <inheritdoc /> |
| 40 | + public override void OnApplyTemplate() |
| 41 | + { |
| 42 | + base.OnApplyTemplate(); |
| 43 | + itemsList = GetTemplateChild("AttachmentList") as ListBox; |
| 44 | + if (itemsList != null) |
| 45 | + { |
| 46 | + itemsList.SelectionMode = SelectionMode.Single; |
| 47 | + itemsList.SelectionChanged += ItemsList_SelectionChanged; |
| 48 | + LoadAttachments(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e) |
| 53 | + { |
| 54 | + if(e.AddedItems != null && e.AddedItems.Count > 0) |
| 55 | + { |
| 56 | + var attachment = e.AddedItems[0] as PopupAttachment; |
| 57 | + if(attachment?.Attachment != null) |
| 58 | + { |
| 59 | + OnAttachmentClicked(attachment); |
| 60 | + } |
| 61 | + if (sender is Selector s) |
| 62 | + s.SelectedValue = null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Occurs when an attachment is clicked. |
| 68 | + /// </summary> |
| 69 | + /// <remarks>Override this to prevent the default "save to file dialog" action.</remarks> |
| 70 | + /// <param name="attachment">Attachment clicked.</param> |
| 71 | + public virtual async void OnAttachmentClicked(PopupAttachment attachment) |
| 72 | + { |
| 73 | + SaveFileDialog saveFileDialog = new SaveFileDialog(); |
| 74 | + saveFileDialog.FileName = attachment.Name; |
| 75 | + if (saveFileDialog.ShowDialog() == true) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + using var stream = await attachment.Attachment!.GetDataAsync(); |
| 80 | + using var outfile = saveFileDialog.OpenFile(); |
| 81 | + await stream.CopyToAsync(outfile); |
| 82 | + } |
| 83 | + catch { } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private async void LoadAttachments() |
| 88 | + { |
| 89 | + if (itemsList is null) return; |
| 90 | + Visibility = Visibility.Collapsed; |
| 91 | + itemsList.ItemsSource = null; |
| 92 | + if (Element is not null) |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + await Element.GetAttachmentsAsync(); |
| 97 | + } |
| 98 | + catch |
| 99 | + { |
| 100 | + |
| 101 | + } |
| 102 | + itemsList.ItemsSource = Element?.Attachments; |
| 103 | + } |
| 104 | + Visibility = (Element?.Attachments?.Count ?? 0) > 0 ? Visibility = Visibility.Visible : Visibility.Collapsed; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Gets or sets the AttachmentsPopupElement. |
| 109 | + /// </summary> |
| 110 | + public AttachmentsPopupElement? Element |
| 111 | + { |
| 112 | + get { return GetValue(ElementProperty) as AttachmentsPopupElement; } |
| 113 | + set { SetValue(ElementProperty, value); } |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Identifies the <see cref="Element"/> dependency property. |
| 118 | + /// </summary> |
| 119 | + public static readonly DependencyProperty ElementProperty = |
| 120 | + DependencyProperty.Register(nameof(Element), typeof(AttachmentsPopupElement), typeof(AttachmentsPopupElementView), new PropertyMetadata(null, (s, e) => ((AttachmentsPopupElementView)s).LoadAttachments())); |
| 121 | + } |
| 122 | +} |
0 commit comments