|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 6 | +using Windows.Foundation; |
| 7 | +using Windows.Foundation.Collections; |
| 8 | +using Microsoft.UI.Xaml; |
| 9 | +using Microsoft.UI.Xaml.Controls; |
| 10 | +using Microsoft.UI.Xaml.Controls.Primitives; |
| 11 | +using Microsoft.UI.Xaml.Data; |
| 12 | +using Microsoft.UI.Xaml.Input; |
| 13 | +using Microsoft.UI.Xaml.Media; |
| 14 | +using Microsoft.UI.Xaml.Navigation; |
| 15 | +using Simple_QR_Code_Maker.Models; |
| 16 | +using Microsoft.UI.Xaml.Media.Imaging; |
| 17 | +using Simple_QR_Code_Maker.Helpers; |
| 18 | +using Windows.ApplicationModel.DataTransfer; |
| 19 | +using Windows.Storage; |
| 20 | +using Simple_QR_Code_Maker.Extensions; |
| 21 | +using CommunityToolkit.Mvvm.Messaging; |
| 22 | + |
| 23 | + |
| 24 | +namespace Simple_QR_Code_Maker.Controls; |
| 25 | + |
| 26 | +public sealed partial class QrCodeImageControl : UserControl |
| 27 | +{ |
| 28 | + public BarcodeImageItem Data |
| 29 | + { |
| 30 | + get { return (BarcodeImageItem)GetValue(DataProperty); } |
| 31 | + set { SetValue(DataProperty, value); } |
| 32 | + } |
| 33 | + |
| 34 | + public static readonly DependencyProperty DataProperty = |
| 35 | + DependencyProperty.Register("Data", typeof(BarcodeImageItem), typeof(QrCodeImageControl), new PropertyMetadata(null)); |
| 36 | + |
| 37 | + |
| 38 | + public QrCodeImageControl() |
| 39 | + { |
| 40 | + InitializeComponent(); |
| 41 | + } |
| 42 | + |
| 43 | + private async void QrCodeImage_DragStarting(UIElement sender, DragStartingEventArgs args) |
| 44 | + { |
| 45 | + if (sender is not Image image || image.Source is not WriteableBitmap bitmap) |
| 46 | + return; |
| 47 | + |
| 48 | + DragOperationDeferral deferral = args.GetDeferral(); |
| 49 | + StorageFolder folder = ApplicationData.Current.LocalCacheFolder; |
| 50 | + string? imageNameFileName = $"{ToolTipService.GetToolTip(image)}" ?? "QR_Code"; |
| 51 | + // remove characters that are not allowed in file names |
| 52 | + imageNameFileName = imageNameFileName.ToSafeFileName(); |
| 53 | + imageNameFileName += ".png"; |
| 54 | + StorageFile file = await folder.CreateFileAsync(imageNameFileName, CreationCollisionOption.ReplaceExisting); |
| 55 | + bool success = await bitmap.SavePngToStorageFile(file); |
| 56 | + |
| 57 | + if (!success) |
| 58 | + { |
| 59 | + deferral.Complete(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + WeakReferenceMessenger.Default.Send(new SaveHistoryMessage()); |
| 64 | + args.Data.SetStorageItems(new[] { file }); |
| 65 | + args.Data.RequestedOperation = DataPackageOperation.Copy; |
| 66 | + deferral.Complete(); |
| 67 | + } |
| 68 | +} |
0 commit comments