private async void OnDrop(object sender, DragEventArgs e)
        {
            // 记得获取Deferral对象
                        //var def = e.GetDeferral();
            if (sender == imgQR || sender == rectDrop)
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    if (items.Count > 0)
                    {
                        var storageFile = items[0] as StorageFile;
                        if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            if (storageFile.FileType.ToLower().Equals(".svg"))
                            {
                                var bitmapImage = new SvgImageSource();

                                if (e.DataView.Contains(StandardDataFormats.WebLink))
                                {
                                    var url = await e.DataView.GetWebLinkAsync();

                                    var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                    var svg = await SVG.CreateFromStream(rms);

                                    bitmapImage = svg.Source;
                                    imgQR.Tag   = svg.Bytes;

                                    //await bitmapImage.SetSourceAsync(rms);
                                    //await rms.FlushAsync();

                                    //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0));
                                    //imgQR.Tag = bytes;
                                }
                                else if (e.DataView.Contains(StandardDataFormats.Text))
                                {
                                    //var content = await e.DataView.GetHtmlFormatAsync();
                                    var content = await e.DataView.GetTextAsync();

                                    if (content.Length > 0)
                                    {
                                        var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                        var svg = await SVG.CreateFromStream(rms);

                                        bitmapImage = svg.Source;
                                        imgQR.Tag   = svg.Bytes;

                                        //await bitmapImage.SetSourceAsync(rms);
                                        //await rms.FlushAsync();

                                        //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0));
                                        //imgQR.Tag = bytes;
                                    }
                                }
                                else
                                {
                                    var svg = await SVG.CreateFromStorageFile(storageFile);

                                    bitmapImage = svg.Source;
                                    imgQR.Tag   = svg.Bytes;

                                    //await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                                    //byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile));
                                    //imgQR.Tag = bytes;
                                }
                                imgQR.Source = bitmapImage;
                            }
                            else
                            {
                                var bitmapImage = new WriteableBitmap(1, 1);

                                if (e.DataView.Contains(StandardDataFormats.WebLink))
                                {
                                    var url = await e.DataView.GetWebLinkAsync();

                                    await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync());
                                }
                                else if (e.DataView.Contains(StandardDataFormats.Text))
                                {
                                    //var content = await e.DataView.GetHtmlFormatAsync();
                                    var content = await e.DataView.GetTextAsync();

                                    if (content.Length > 0)
                                    {
                                        await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync());
                                    }
                                }
                                else
                                {
                                    await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                                }

                                byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length);
                                imgQR.Source = bitmapImage;
                            }
                        }
                    }
                }
                else if (e.DataView.Contains(StandardDataFormats.WebLink))
                {
                    var uri = await e.DataView.GetWebLinkAsync();

                    StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

                    if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                    {
                        if (storageFile.FileType.ToLower().Equals(".svg"))
                        {
                            var bitmapImage = new SvgImageSource();

                            if (e.DataView.Contains(StandardDataFormats.WebLink))
                            {
                                var url = await e.DataView.GetWebLinkAsync();

                                var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                var svg = await SVG.CreateFromStream(rms);

                                bitmapImage = svg.Source;
                                imgQR.Tag   = svg.Bytes;

                                //await bitmapImage.SetSourceAsync(rms);
                                //await rms.FlushAsync();

                                //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0));
                                //imgQR.Tag = bytes;
                            }
                            else if (e.DataView.Contains(StandardDataFormats.Text))
                            {
                                //var content = await e.DataView.GetHtmlFormatAsync();
                                var content = await e.DataView.GetTextAsync();

                                if (content.Length > 0)
                                {
                                    var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                    var svg = await SVG.CreateFromStream(rms);

                                    bitmapImage = svg.Source;
                                    imgQR.Tag   = svg.Bytes;

                                    //await bitmapImage.SetSourceAsync(rms);
                                    //await rms.FlushAsync();

                                    //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0));
                                    //imgQR.Tag = bytes;
                                }
                            }
                            else
                            {
                                var svg = await SVG.CreateFromStorageFile(storageFile);

                                bitmapImage = svg.Source;
                                imgQR.Tag   = svg.Bytes;

                                //await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                                //byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile));
                                //imgQR.Tag = bytes;
                            }
                            imgQR.Source = bitmapImage;
                        }
                        else
                        {
                            var bitmapImage = new WriteableBitmap(1, 1);

                            if (e.DataView.Contains(StandardDataFormats.WebLink))
                            {
                                var url = await e.DataView.GetWebLinkAsync();

                                await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync());
                            }
                            else if (e.DataView.Contains(StandardDataFormats.Text))
                            {
                                //var content = await e.DataView.GetHtmlFormatAsync();
                                var content = await e.DataView.GetTextAsync();

                                if (content.Length > 0)
                                {
                                    await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync());
                                }
                            }
                            else
                            {
                                await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                            }

                            byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length);
                            imgQR.Source = bitmapImage;
                        }
                    }
                    //var bitmapImage = new WriteableBitmap(1, 1);
                    //await bitmapImage.SetSourceAsync(await file.OpenAsync(FileAccessMode.Read));
                    //// Set the image on the main page to the dropped image
                    //byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length);
                    //imgQR.Source = bitmapImage;
                }
            }
            else if (sender == edQR)
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    if (items.Count > 0)
                    {
                        var storageFile = items[0] as StorageFile;
                        if (Utils.text_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            edQR.Text = await FileIO.ReadTextAsync(storageFile);
                        }
                    }
                }
                else if (e.DataView.Contains(StandardDataFormats.Text) ||
                         e.DataView.Contains(StandardDataFormats.WebLink) ||
                         e.DataView.Contains(StandardDataFormats.Html) ||
                         e.DataView.Contains(StandardDataFormats.Rtf))
                {
                    var content = await e.DataView.GetTextAsync();

                    if (content.Length > 0)
                    {
                        edQR.Text = content;
                    }
                }
            }
            //def.Complete();
        }
Beispiel #2
0
        private async void OnDrop(object sender, DragEventArgs e)
        {
            // 需要异步拖放时记得获取Deferral对象
                        //var def = e.GetDeferral();
            if (sender == imgSvg || sender == BackgroundCanvas)
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    if (items.Count > 0)
                    {
                        try
                        {
                            var storageFile = items[0] as StorageFile;
                            SourceFileName = storageFile.Name;
                            if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                            {
                                if (storageFile.FileType.ToLower().Equals(".svg"))
                                {
                                    var bitmapImage = new SvgImageSource();
                                    if (e.DataView.Contains(StandardDataFormats.WebLink))
                                    {
                                        var url = await e.DataView.GetWebLinkAsync();

                                        var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                        var svg = await SVG.CreateFromStream(rms);

                                        bitmapImage = svg.Source;
                                        imgSvg.Tag  = svg.Bytes;
                                    }
                                    else if (e.DataView.Contains(StandardDataFormats.Text))
                                    {
                                        var content = await e.DataView.GetTextAsync();

                                        if (content.Length > 0)
                                        {
                                            var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                            var svg = await SVG.CreateFromStream(rms);

                                            bitmapImage = svg.Source;
                                            imgSvg.Tag  = svg.Bytes;
                                        }
                                    }
                                    else
                                    {
                                        var svg = await SVG.CreateFromStorageFile(storageFile);

                                        bitmapImage = svg.Source;
                                        imgSvg.Tag  = svg.Bytes;
                                    }
                                    imgSvg.Source            = bitmapImage;
                                    viewSvg.StretchDirection = StretchDirection.Both;
                                }
                                else if (storageFile.FileType.ToLower().Equals(".xaml"))
                                {
                                    //var xaml = await FileIO.ReadTextAsync(storageFile);
                                    //var ui = xaml.LoadXAML();
                                }
                                else
                                {
                                    imgSvg.Source = await storageFile.ToWriteableBitmap();

                                    viewSvg.StretchDirection = StretchDirection.DownOnly;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            await new MessageDialog(ex.Message.T(), "ERROR".T()).ShowAsync();
                        }
                    }
                }
                else if (e.DataView.Contains(StandardDataFormats.WebLink))
                {
                    var uri = await e.DataView.GetWebLinkAsync();

                    try
                    {
                        StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

                        SourceFileName = storageFile.Name;
                        if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            if (storageFile.FileType.ToLower().Equals(".svg"))
                            {
                                var bitmapImage = new SvgImageSource();

                                if (e.DataView.Contains(StandardDataFormats.WebLink))
                                {
                                    var url = await e.DataView.GetWebLinkAsync();

                                    var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                    var svg = await SVG.CreateFromStream(rms);

                                    bitmapImage = svg.Source;
                                    imgSvg.Tag  = svg.Bytes;
                                }
                                else if (e.DataView.Contains(StandardDataFormats.Text))
                                {
                                    var content = await e.DataView.GetTextAsync();

                                    if (content.Length > 0)
                                    {
                                        var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                        var svg = await SVG.CreateFromStream(rms);

                                        bitmapImage = svg.Source;
                                        imgSvg.Tag  = svg.Bytes;
                                    }
                                }
                                else
                                {
                                    var svg = await SVG.CreateFromStorageFile(storageFile);

                                    bitmapImage = svg.Source;
                                    imgSvg.Tag  = svg.Bytes;
                                }
                                imgSvg.Source = bitmapImage;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        await new MessageDialog(ex.Message.T(), "ERROR".T()).ShowAsync();
                    }
                }
            }
            //def.Complete();
        }
Beispiel #3
0
        private async void OnDrop(object sender, DragEventArgs e)
        {
            // 需要异步拖放时记得获取Deferral对象
                        //var def = e.GetDeferral();
            if (sender == imgBase64 || sender == rectDrop)
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    if (items.Count > 0)
                    {
                        var storageFile = items[0] as StorageFile;
                        if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            if (storageFile.FileType.ToLower().Equals(".svg"))
                            {
                                var bitmapImage = new SvgImageSource();
                                if (e.DataView.Contains(StandardDataFormats.WebLink))
                                {
                                    var url = await e.DataView.GetWebLinkAsync();

                                    var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                    var svg = await SVG.CreateFromStream(rms);

                                    bitmapImage   = svg.Source;
                                    imgBase64.Tag = svg.Bytes;
                                }
                                else if (e.DataView.Contains(StandardDataFormats.Text))
                                {
                                    var content = await e.DataView.GetTextAsync();

                                    if (content.Length > 0)
                                    {
                                        var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                        var svg = await SVG.CreateFromStream(rms);

                                        bitmapImage   = svg.Source;
                                        imgBase64.Tag = svg.Bytes;
                                    }
                                }
                                else
                                {
                                    var svg = await SVG.CreateFromStorageFile(storageFile);

                                    bitmapImage   = svg.Source;
                                    imgBase64.Tag = svg.Bytes;
                                }
                                imgBase64.Source = bitmapImage;
                            }
                            else
                            {
                                var bitmapImage = new WriteableBitmap(1, 1);

                                if (e.DataView.Contains(StandardDataFormats.WebLink))
                                {
                                    var url = await e.DataView.GetWebLinkAsync();

                                    await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync());
                                }
                                else if (e.DataView.Contains(StandardDataFormats.Text))
                                {
                                    //var content = await e.DataView.GetHtmlFormatAsync();
                                    var content = await e.DataView.GetTextAsync();

                                    if (content.Length > 0)
                                    {
                                        await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync());
                                    }
                                }
                                else
                                {
                                    await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                                }

                                byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length);
                                imgBase64.Source = bitmapImage;
                            }
                        }
                    }
                }
                else if (e.DataView.Contains(StandardDataFormats.WebLink))
                {
                    var uri = await e.DataView.GetWebLinkAsync();

                    StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

                    if (Utils.image_ext.Contains(storageFile.FileType.ToLower()))
                    {
                        if (storageFile.FileType.ToLower().Equals(".svg"))
                        {
                            var bitmapImage = new SvgImageSource();

                            if (e.DataView.Contains(StandardDataFormats.WebLink))
                            {
                                var url = await e.DataView.GetWebLinkAsync();

                                var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync();

                                var svg = await SVG.CreateFromStream(rms);

                                bitmapImage   = svg.Source;
                                imgBase64.Tag = svg.Bytes;
                            }
                            else if (e.DataView.Contains(StandardDataFormats.Text))
                            {
                                //var content = await e.DataView.GetHtmlFormatAsync();
                                var content = await e.DataView.GetTextAsync();

                                if (content.Length > 0)
                                {
                                    var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync();

                                    var svg = await SVG.CreateFromStream(rms);

                                    bitmapImage   = svg.Source;
                                    imgBase64.Tag = svg.Bytes;
                                }
                            }
                            else
                            {
                                await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());

                                byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile));
                                imgBase64.Tag = bytes;
                            }
                            imgBase64.Source = bitmapImage;
                        }
                        else
                        {
                            var bitmapImage = new WriteableBitmap(1, 1);

                            if (e.DataView.Contains(StandardDataFormats.WebLink))
                            {
                                var url = await e.DataView.GetWebLinkAsync();

                                await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync());
                            }
                            else if (e.DataView.Contains(StandardDataFormats.Text))
                            {
                                var content = await e.DataView.GetTextAsync();

                                if (content.Length > 0)
                                {
                                    await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync());
                                }
                            }
                            else
                            {
                                await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync());
                            }

                            byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length);
                            imgBase64.Source = bitmapImage;
                        }
                    }
                }
            }
            else if (sender == edBase64)
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    if (items.Count > 0)
                    {
                        var storageFile = items[0] as StorageFile;
                        if (Utils.url_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            if (e.DataView.Contains(StandardDataFormats.WebLink))
                            {
                                var url = await e.DataView.GetWebLinkAsync();

                                if (url.IsAbsoluteUri)
                                {
                                    edBase64.Text = url.ToString();
                                }
                                else if (url.IsFile || url.IsUnc)
                                {
                                    edBase64.Text = await FileIO.ReadTextAsync(storageFile);
                                }
                            }
                            else if (e.DataView.Contains(StandardDataFormats.Text))
                            {
                                //var content = await e.DataView.GetHtmlFormatAsync();
                                var content = await e.DataView.GetTextAsync();

                                if (content.Length > 0)
                                {
                                    edBase64.Text = content;
                                }
                            }
                        }
                        else if (Utils.text_ext.Contains(storageFile.FileType.ToLower()))
                        {
                            edBase64.Text = await FileIO.ReadTextAsync(storageFile);
                        }
                    }
                }
                else if (e.DataView.Contains(StandardDataFormats.WebLink))
                {
                    var uri = await e.DataView.GetWebLinkAsync();

                    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

                    if (!file.FileType.ToLower().Equals(".txt"))
                    {
                        return;
                    }
                    edBase64.Text = await FileIO.ReadTextAsync(file);
                }
                else if (e.DataView.Contains(StandardDataFormats.Text))
                {
                    var content = await e.DataView.GetTextAsync();

                    if (content.Length > 0)
                    {
                        edBase64.Text = content;
                    }
                }
            }
            await IsSVG(imgBase64);

            //def.Complete();
        }