Ejemplo n.º 1
0
        private static async Task ProcessComandsAsync(DropConfiguration configuration, DataPackageView dataview)
        {
            if (configuration.OnDropDataViewCommand != null)
            {
                configuration.OnDropDataViewCommand.Execute(dataview);
            }


            if (dataview.Contains(StandardDataFormats.ApplicationLink) && configuration.OnDropApplicationLinkCommand != null)
            {
                var uri = await dataview.GetApplicationLinkAsync();

                configuration.OnDropApplicationLinkCommand.Execute(uri);
            }

            if (dataview.Contains(StandardDataFormats.Bitmap) && configuration.OnDropBitmapCommand != null)
            {
                var stream = await dataview.GetBitmapAsync();

                configuration.OnDropBitmapCommand.Execute(stream);
            }

            if (dataview.Contains(StandardDataFormats.Html) && configuration.OnDropHtmlCommand != null)
            {
                var html = await dataview.GetHtmlFormatAsync();

                configuration.OnDropHtmlCommand.Execute(html);
            }

            if (dataview.Contains(StandardDataFormats.Rtf) && configuration.OnDropRtfCommand != null)
            {
                var rtf = await dataview.GetRtfAsync();

                configuration.OnDropRtfCommand.Execute(rtf);
            }

            if (dataview.Contains(StandardDataFormats.StorageItems) && configuration.OnDropStorageItemsCommand != null)
            {
                var storageItems = await dataview.GetStorageItemsAsync();

                configuration.OnDropStorageItemsCommand.Execute(storageItems);
            }

            if (dataview.Contains(StandardDataFormats.Text) && configuration.OnDropTextCommand != null)
            {
                var text = await dataview.GetTextAsync();

                configuration.OnDropTextCommand.Execute(text);
            }

            if (dataview.Contains(StandardDataFormats.WebLink) && configuration.OnDropWebLinkCommand != null)
            {
                var uri = await dataview.GetWebLinkAsync();

                configuration.OnDropWebLinkCommand.Execute(uri);
            }
        }
        public async Task ProcessComandsAsync(DataPackageView dataview)
        {
            if (DropDataViewAction != null)
            {
                DropDataViewAction.Invoke(dataview);
            }

            if (dataview.Contains(StandardDataFormats.ApplicationLink) && DropApplicationLinkAction != null)
            {
                Uri uri = await dataview.GetApplicationLinkAsync();

                DropApplicationLinkAction.Invoke(uri);
            }

            if (dataview.Contains(StandardDataFormats.Bitmap) && DropBitmapAction != null)
            {
                RandomAccessStreamReference stream = await dataview.GetBitmapAsync();

                DropBitmapAction.Invoke(stream);
            }

            if (dataview.Contains(StandardDataFormats.Html) && DropHtmlAction != null)
            {
                string html = await dataview.GetHtmlFormatAsync();

                DropHtmlAction.Invoke(html);
            }

            if (dataview.Contains(StandardDataFormats.Rtf) && DropRtfAction != null)
            {
                string rtf = await dataview.GetRtfAsync();

                DropRtfAction.Invoke(rtf);
            }

            if (dataview.Contains(StandardDataFormats.StorageItems) && DropStorageItemsAction != null)
            {
                IReadOnlyList <IStorageItem> storageItems = await dataview.GetStorageItemsAsync();

                DropStorageItemsAction.Invoke(storageItems);
            }

            if (dataview.Contains(StandardDataFormats.Text) && DropTextAction != null)
            {
                string text = await dataview.GetTextAsync();

                DropTextAction.Invoke(text);
            }

            if (dataview.Contains(StandardDataFormats.WebLink) && DropWebLinkAction != null)
            {
                Uri uri = await dataview.GetWebLinkAsync();

                DropWebLinkAction.Invoke(uri);
            }
        }
Ejemplo n.º 3
0
        public async Task ProcessComandsAsync(DataPackageView dataview)
        {
            if (DropDataViewAction != null)
            {
                DropDataViewAction.Invoke(dataview);
            }

            if (dataview.Contains(StandardDataFormats.ApplicationLink) && DropApplicationLinkAction != null)
            {
                var uri = await dataview.GetApplicationLinkAsync();

                DropApplicationLinkAction.Invoke(uri);
            }

            if (dataview.Contains(StandardDataFormats.Bitmap) && DropBitmapAction != null)
            {
                var stream = await dataview.GetBitmapAsync();

                DropBitmapAction.Invoke(stream);
            }

            if (dataview.Contains(StandardDataFormats.Html) && DropHtmlAction != null)
            {
                var html = await dataview.GetHtmlFormatAsync();

                DropHtmlAction.Invoke(html);
            }

            if (dataview.Contains(StandardDataFormats.Rtf) && DropRtfAction != null)
            {
                var rtf = await dataview.GetRtfAsync();

                DropRtfAction.Invoke(rtf);
            }

            if (dataview.Contains(StandardDataFormats.StorageItems) && DropStorageItemsAction != null)
            {
                var storageItems = await dataview.GetStorageItemsAsync();

                DropStorageItemsAction.Invoke(storageItems);
            }

            if (dataview.Contains(StandardDataFormats.Text) && DropTextAction != null)
            {
                var text = await dataview.GetTextAsync();

                DropTextAction.Invoke(text);
            }

            if (dataview.Contains(StandardDataFormats.WebLink) && DropWebLinkAction != null)
            {
                var uri = await dataview.GetWebLinkAsync();

                DropWebLinkAction.Invoke(uri);
            }
        }
Ejemplo n.º 4
0
        static string GetRichTextFormat(DataPackageView dpv)
        {
            var task = Task <string> .Run(async() =>
            {
                var rtf = await dpv.GetRtfAsync();
                return(rtf);
            });

            return(task.Result);
        }
Ejemplo n.º 5
0
        private async void LayoutRoot_Drop(object sender, DragEventArgs e)
        {
            var defer = e.GetDeferral();

            try
            {
                DataPackageView dataView = e.DataView;
                if (dataView.Contains(StandardDataFormats.StorageItems))
                {
                    var files = await dataView.GetStorageItemsAsync();

                    var file = files.OfType <StorageFile>().First();
                    if (file.FileType == ".png" || file.FileType == ".jpg" || file.FileType == ".bmp")
                    {
                        this.file = file;
                        var bi = new BitmapImage();
                        IRandomAccessStream s = await file.OpenAsync(FileAccessMode.Read);

                        await bi.SetSourceAsync(s);

                        photo.Source = bi;
                        mode         = SendMode.Photo;
                    }
                    else if (file.FileType == ".txt")
                    {
                        string text = await FileIO.ReadTextAsync(file);

                        send.Text = text.Length > 140 ? text.Substring(0, 140) : text;
                    }
                }
                else if (dataView.Contains(StandardDataFormats.Text))
                {
                    string text = await dataView.GetTextAsync();

                    send.Text = text.Length > 140 ? text.Substring(0, 140) : text;
                }
                else if (dataView.Contains(StandardDataFormats.Rtf))
                {
                    string text = await dataView.GetRtfAsync();

                    send.Text = text.Length > 140 ? text.Substring(0, 140) : text;
                }
            }
            finally
            {
                defer.Complete();
            }
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 IAsyncOperation <string> IDataPackageViewResolver.GetRtfAsync(DataPackageView dataPackageView) => dataPackageView.GetRtfAsync();
Ejemplo n.º 8
0
        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.
                }
            }
        }