private async void OnKeyboards(CoreWindow sender, KeyEventArgs args) { try { CoreVirtualKeyStates controlKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control); var isCtrlDown = (controlKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down; CoreVirtualKeyStates shiftKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift); var isShiftDown = (shiftKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down; var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control); var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift); switch (args.VirtualKey) { case VirtualKey.Escape: //NavigationService.GoBack(); break; } if (isCtrlDown && args.VirtualKey == VirtualKey.Left || args.VirtualKey == VirtualKey.Escape) { NavigationService.GoBack(); } if (isCtrlDown && args.VirtualKey == VirtualKey.V || isShiftDown && args.VirtualKey == VirtualKey.Insert) { if (InstaApi != null && InstaApi.IsUserAuthenticated) { DataPackageView dataPackageView = Clipboard.GetContent(); if (dataPackageView.Contains(StandardDataFormats.StorageItems)) { var items = await dataPackageView.GetStorageItemsAsync(); if (items.Count > 0) { if (items[0] is StorageFile file) { if (file.Path.IsSupportedImage()) // IsSupportedVideo ? { if (NavigationService.Frame.Content is Views.Direct.ThreadView thread) { thread.UploadFile(file); } else { await new ContentDialogs.FileAssociationDialog(file).ShowAsync(); } } else { ShowNotify("This file is not supported.\r\n" + file.Path, 3000); } } } } else if (dataPackageView.Contains(StandardDataFormats.Bitmap)) { var bitmap = await dataPackageView.GetBitmapAsync(); var decoder = await BitmapDecoder.CreateAsync(await bitmap.OpenReadAsync()); var file = await GenerateRandomOutputFile(); var encoder = await BitmapEncoder.CreateForTranscodingAsync(await file.OpenAsync(FileAccessMode.ReadWrite), decoder); await encoder.FlushAsync(); if (NavigationService.Frame.Content is Views.Direct.ThreadView thread) { thread.UploadFile(file); } else { await new ContentDialogs.FileAssociationDialog(file).ShowAsync(); } } } } } catch { } }
private static async Task <DataObject> ToDataObject(DataPackageView src, CancellationToken ct) { var dst = new DataObject(); // WPF has no format for URI therefore text is used for both if (src.Contains(StandardDataFormats.Text)) { dst.SetText(await src.GetTextAsync().AsTask(ct)); } else { var uri = DataPackage.CombineUri( src.Contains(StandardDataFormats.WebLink) ? (await src.GetWebLinkAsync().AsTask(ct)).ToString() : null, src.Contains(StandardDataFormats.ApplicationLink) ? (await src.GetApplicationLinkAsync().AsTask(ct)).ToString() : null, src.Contains(StandardDataFormats.Uri) ? (await src.GetUriAsync().AsTask(ct)).ToString() : null); if (string.IsNullOrEmpty(uri) == false) { dst.SetText(uri); } } if (src.Contains(StandardDataFormats.Html)) { dst.SetData(DataFormats.Html, await src.GetHtmlFormatAsync().AsTask(ct)); } if (src.Contains(StandardDataFormats.Rtf)) { dst.SetData(DataFormats.Rtf, await src.GetRtfAsync().AsTask(ct)); } if (src.Contains(StandardDataFormats.Bitmap)) { var srcStreamRef = await src.GetBitmapAsync().AsTask(ct); var srcStream = await srcStreamRef.OpenReadAsync().AsTask(ct); // We copy the source stream in memory to avoid marshalling issue with async stream // and to make sure to read it async as it built from a RandomAccessStream which might be remote. using var tmp = new MemoryStream(); await srcStream.AsStreamForRead().CopyToAsync(tmp); tmp.Position = 0; var dstBitmap = new BitmapImage(); dstBitmap.BeginInit(); dstBitmap.CreateOptions = BitmapCreateOptions.None; dstBitmap.CacheOption = BitmapCacheOption.OnLoad; // Required for the BitmapImage to internally cache the data (so we can dispose the tmp stream) dstBitmap.StreamSource = tmp; dstBitmap.EndInit(); dst.SetData(DataFormats.Bitmap, dstBitmap, false); } if (src.Contains(StandardDataFormats.StorageItems)) { var files = await src.GetStorageItemsAsync().AsTask(ct); var paths = new StringCollection(); foreach (var item in files) { paths.Add(item.Path); } dst.SetFileDropList(paths); } return(dst); }
IAsyncOperation <RandomAccessStreamReference> IDataPackageViewResolver.GetBitmapAsync(DataPackageView dataPackageView) => dataPackageView.GetBitmapAsync();
public static async Task CopyTo(this DataPackageView view, IDictionary<string, object> values) { foreach (var format in view.AvailableFormats) { try { if (format == StandardDataFormats.Bitmap) { var value = await view.GetBitmapAsync(); values.Add(format, value); } else if (format == StandardDataFormats.Html) { var value = await view.GetHtmlFormatAsync(); values.Add(format, value); if (!values.ContainsKey(StandardDataFormats.Text)) values.Add(StandardDataFormats.Text, value); } else if (format == StandardDataFormats.Rtf) { var value = await view.GetRtfAsync(); values.Add(format, value); if (!values.ContainsKey(StandardDataFormats.Text)) values.Add(StandardDataFormats.Text, value); } else if (format == StandardDataFormats.StorageItems) { var value = await view.GetStorageItemsAsync(); var extensions = value.Select(f => "*" + Path.GetExtension(f.Name)).ToArray(); values.Add("extensions", extensions); values.Add(format, value); if (value.Count() == 1) { var file = value.OfType<StorageFile>().SingleOrDefault(); if(file != null && file.Attributes.HasFlag(FileAttributes.Temporary)) { var copiedFile = await file.CopyAsync( ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting); values[format] = new[] { copiedFile }; } if (!values.ContainsKey(StandardDataFormats.Bitmap)) { var stream = RandomAccessStreamReference.CreateFromFile(file); values.Add(StandardDataFormats.Bitmap, stream); } } } else if (format == StandardDataFormats.Text) { var value = await view.GetTextAsync(); values.Add(format, value); } else if (format == StandardDataFormats.ApplicationLink) { var value = await view.GetApplicationLinkAsync(); values.Add(format, value); if (!values.ContainsKey(StandardDataFormats.Text)) values.Add(StandardDataFormats.Text, value.ToString()); } else if (format == StandardDataFormats.WebLink) { var value = await view.GetWebLinkAsync(); values.Add(format, value); if (!values.ContainsKey(StandardDataFormats.Text)) values.Add(StandardDataFormats.Text, value.ToString()); } else if (ValidFormat(format)) { var value = await view.GetDataAsync(format); values.Add(format, value); } } catch (SecurityException) { throw; } catch { // Some formats are incompatible with WinRT and can cause it to throw unexpectedly. // guard against it and give a best effort copy of the available formats that are supported. } } }
private static async Task <ReceivedShareItem> FetchDataFromPackageViewAsync(DataPackageView packageView) { var rval = new ReceivedShareItem() { Title = packageView.Properties.Title, Description = packageView.Properties.Description, PackageFamilyName = packageView.Properties.PackageFamilyName, ContentSourceWebLink = packageView.Properties.ContentSourceWebLink, ContentSourceApplicationLink = packageView.Properties.ContentSourceApplicationLink, LogoBackgroundColor = packageView.Properties.LogoBackgroundColor, }; if (packageView.Properties.Square30x30Logo != null) { using (var logoStream = await packageView.Properties.Square30x30Logo.OpenReadAsync()) { var logo = new MemoryStream(); await logoStream.AsStreamForRead().CopyToAsync(logo); logo.Position = 0; var str = Convert.ToBase64String(logo.ToArray()); //rval.Square30x30LogoBase64 = Convert.ToBase64String(logo.ToArray()); rval.Square30x30Logo = new Models.MemoryStreamBase64Item { Base64String = str }; } } if (packageView.Properties.Thumbnail != null) { using (var thumbnailStream = await packageView.Properties.Thumbnail.OpenReadAsync()) { var thumbnail = new MemoryStream(); await thumbnailStream.AsStreamForRead().CopyToAsync(thumbnail); thumbnail.Position = 0; var str = Convert.ToBase64String(thumbnail.ToArray()); rval.Thumbnail = new Models.MemoryStreamBase64Item { Base64String = str }; } } if (packageView.Contains(StandardDataFormats.WebLink)) { try { var link = new WebLinkShareItem { WebLink = await packageView.GetWebLinkAsync() }; rval.AvialableShareItems.Add(link); } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetWebLinkAsync - " + ex.Message, NotifyType.ErrorMessage); } } if (packageView.Contains(StandardDataFormats.ApplicationLink)) { try { var sharedApplicationLink = new ApplicationLinkShareItem { ApplicationLink = await packageView.GetApplicationLinkAsync() }; rval.AvialableShareItems.Add(sharedApplicationLink); } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetApplicationLinkAsync - " + ex.Message, NotifyType.ErrorMessage); } } if (packageView.Contains(StandardDataFormats.Text)) { try { var sharedText = new TextShareItem { Text = await packageView.GetTextAsync() }; rval.AvialableShareItems.Add(sharedText); rval.Text = await packageView.GetTextAsync(); //rval.GetValueContainer(x => x.Text) // .GetNullObservable() // .Subscribe(e => sharedText.Text = rval.Text) // .DisposeWith(rval); sharedText.GetValueContainer(x => x.Text) .GetNullObservable() .Subscribe(e => rval.Text = sharedText.Text) .DisposeWith(rval); } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetTextAsync - " + ex.Message, NotifyType.ErrorMessage); } } if (packageView.Contains(StandardDataFormats.StorageItems)) { try { var files = await packageView.GetStorageItemsAsync(); var sharedStorageItem = new FilesShareItem { StorageFiles = new ObservableCollection <FileItem>() //StorageItems = }; foreach (StorageFile sf in files) { var guidString = Guid.NewGuid().ToString(); StorageApplicationPermissions.FutureAccessList.AddOrReplace(guidString, sf, sf.Name); var ts = await sf.GetScaledImageAsThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.VideosView); var tmbs = new MemoryStream(); await ts.AsStreamForRead().CopyToAsync(tmbs); var file = new FileItem { AccessToken = guidString, ContentType = sf.ContentType, FileName = sf.DisplayName, PossiblePath = sf.Path, Thumbnail = new Models.MemoryStreamBase64Item(tmbs.ToArray()) }; sharedStorageItem.StorageFiles.Add(file); } //StorageApplicationPermissions.FutureAccessList.AddOrReplace() rval.AvialableShareItems.Add(sharedStorageItem); } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetStorageItemsAsync - " + ex.Message, NotifyType.ErrorMessage); } } //if (packageView.Contains(dataFormatName)) //{ // try // { // this.sharedCustomData = await packageView.GetTextAsync(dataFormatName); // } // catch (Exception ex) // { // //NotifyUserBackgroundThread("Failed GetTextAsync(" + dataFormatName + ") - " + ex.Message, NotifyType.ErrorMessage); // } //} if (packageView.Contains(StandardDataFormats.Html)) { var sharedHtmlFormatItem = new HtmlShareItem(); var sharedHtmlFormat = string.Empty; try { sharedHtmlFormat = await packageView.GetHtmlFormatAsync(); //sharedHtmlFormatItem.HtmlFormat = sharedHtmlFormat; sharedHtmlFormatItem.HtmlFragment = HtmlFormatHelper.GetStaticFragment(sharedHtmlFormat); } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetHtmlFormatAsync - " + ex.Message, NotifyType.ErrorMessage); } //try //{ // var sharedResourceMap = await packageView.GetResourceMapAsync(); //} //catch (Exception ex) //{ // //NotifyUserBackgroundThread("Failed GetResourceMapAsync - " + ex.Message, NotifyType.ErrorMessage); //} //if (packageView.Contains(StandardDataFormats.WebLink)) //{ // try // { // sharedHtmlFormatItem.WebLink = await packageView.GetWebLinkAsync(); // } // catch (Exception ex) // { // //NotifyUserBackgroundThread("Failed GetWebLinkAsync - " + ex.Message, NotifyType.ErrorMessage); // } //} rval.AvialableShareItems.Add(sharedHtmlFormatItem); } if (packageView.Contains(StandardDataFormats.Bitmap)) { try { var fi = await packageView.GetBitmapAsync(); using (var imgFileStream = await fi.OpenReadAsync()) { var saveTargetStream = new InMemoryRandomAccessStream(); var bitmapSourceStream = imgFileStream; await ServiceLocator .Instance .Resolve <IImageConvertService>() .ConverterBitmapToTargetStreamAsync(bitmapSourceStream, saveTargetStream); saveTargetStream.Seek(0); var sr = saveTargetStream.GetInputStreamAt(0); var source = sr.AsStreamForRead(); var ms = new MemoryStream(); await source.CopyToAsync(ms); var sharedBitmapStreamRef = new DelayRenderedImageShareItem { SelectedImage = new Models.MemoryStreamBase64Item(ms.ToArray()) }; rval.AvialableShareItems.Add(sharedBitmapStreamRef); } } catch (Exception ex) { //NotifyUserBackgroundThread("Failed GetBitmapAsync - " + ex.Message, NotifyType.ErrorMessage); } } //foreach (var item in rval.AvialableShareItems) //{ // //item.ContentSourceApplicationLink = rval.ContentSourceApplicationLink; // //item.ContentSourceWebLink = rval.ContentSourceWebLink; // //item.DefaultFailedDisplayText = rval.DefaultFailedDisplayText; // //item.Description = rval.Description; // //item.Title = rval.Title; //} return(rval); }
//康戳键 private void KeyCtrl(VirtualKey value) { switch (value) { //Undo:撤销 case VirtualKey.Z: if (App.Model.isUndo) { Undo(); } break; case VirtualKey.Y: if (App.Model.isRedo) { Redo(); } break; case VirtualKey.S: HomeContentDialog.ShowAsync(); break; //Mask:选区 case VirtualKey.X: 修图.Control.MaskButton.Cut(); break; case VirtualKey.C: 修图.Control.MaskButton.Copy(); break; case VirtualKey.V: DataPackageView dataPackageView = Clipboard.GetContent(); if (dataPackageView.Contains(StandardDataFormats.Bitmap)) { IRandomAccessStreamReference imageReceived = null; imageReceived = dataPackageView.GetBitmapAsync().AsTask().Result; if (imageReceived != null) { using (IRandomAccessStream stream = imageReceived.OpenReadAsync().AsTask().Result) { App.Model.SecondCanvasBitmap = CanvasVirtualBitmap.LoadAsync(App.Model.VirtualControl, stream).AsTask().Result; App.InitializeOther(); LayerControl_Layers(); } } } else if (App.Model.isClipboard == true) { App.InitializeEffect(); MaskButton_Mask(102); } break; case VirtualKey.Delete: 修图.Control.MaskButton.Clear(); break; case VirtualKey.E: 修图.Control.MaskButton.Extract(); break; case VirtualKey.M: 修图.Control.MaskButton.Merge(); break; case VirtualKey.A: 修图.Control.MaskButton.All(); break; case VirtualKey.D: 修图.Control.MaskButton.Deselect(); break; case VirtualKey.P: 修图.Control.MaskButton.Pixel(); break; case VirtualKey.I: 修图.Control.MaskButton.Invert(); break; case VirtualKey.F: MaskButton_Mask(110); break; case VirtualKey.T: MaskButton_Mask(111); break; } }