Contains the data that a user wants to exchange with another app.
Ejemplo n.º 1
0
 internal DataPackageView(DataPackage package)
 {
     _package = package;
     Properties = new DataPackagePropertySetView(package.Properties);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the current content that is stored in the clipboard object.
        /// </summary>
        /// <returns>Contains the content of the Clipboard.</returns>
        public static DataPackageView GetContent()
        {
#if __ANDROID__
            if(_clipboardManager.HasPrimaryClip)
            {
                DataPackage package = new DataPackage();

                if (_clipboardManager.PrimaryClipDescription != null)
                {
                    package.Properties.Description = _clipboardManager.PrimaryClipDescription.Label;
                }

                ClipData data = _clipboardManager.PrimaryClip;
                for (int i = 0; i < data.ItemCount; i++)
                {
                    ClipData.Item item = data.GetItemAt(i);
                    if (!string.IsNullOrEmpty(item.Text))
                    {
                        package.SetText(item.Text);
                    }
                    else if(item.Uri != null)
                    {
                        package.SetWebLink(new Uri(item.Uri.ToString()));
                    }
                }

                return package.GetView();
            }
#elif __IOS__
            if(UIPasteboard.General.Count > 0)
            {
                DataPackage package = new DataPackage();
                var str = UIPasteboard.General.String;
                if(!string.IsNullOrEmpty(str))
                {
                    package.SetText(str);
                }
                var url = UIPasteboard.General.Url;
                if(url != null)
                {
                    package.SetWebLink(new Uri(url.ToString()));
                }

                return package.GetView();
            }

#elif __MAC__
            if(!string.IsNullOrEmpty(NSPasteboard.GeneralPasteboard.GetAvailableTypeFromArray(new string[] { NSPasteboard.NSPasteboardTypeString})))
            {
                DataPackage package = new DataPackage();
                string str = NSPasteboard.GeneralPasteboard.GetStringForType(NSPasteboard.NSPasteboardTypeString);
                if (!string.IsNullOrEmpty(str))
                {
                    package.SetText(str);
                }

                return package.GetView();
            }

#elif WINDOWS_PHONE_APP
            if(_on10)
            {
                var dpv = _type10.GetRuntimeMethod("GetContent", new Type[0]).Invoke(null, new object[0]) as Windows.ApplicationModel.DataTransfer.DataPackageView;
                if(dpv != null)
                {
                    DataPackage package = new DataPackage();
                    
                    if (!string.IsNullOrEmpty(dpv.Properties.Title))
                    {
                        package.Properties.Title = dpv.Properties.Title;
                    }
                    if (!string.IsNullOrEmpty(dpv.Properties.Description))
                    {
                        package.Properties.Description = dpv.Properties.Description;
                    }
                    
                    foreach (string format in dpv.AvailableFormats)
                    {
                        if (format == StandardDataFormats.ApplicationLink || format == StandardDataFormats.WebLink || format == StandardDataFormats.Text)
                        {
                            Task<object> t = Task.Run<object>(async () =>
                            {
                                return await dpv.GetDataAsync(format);
                            });
                            t.Wait();

                            package.SetData(format, t.Result);
                        }

                    }

                    return package.GetView();
                }
            }
#elif WIN32
            string txt = GetText();
            if(!string.IsNullOrEmpty(txt))
            {
                DataPackage package = new DataPackage();
                package.SetText(txt);
                return package.GetView();
            }
#else
            throw new PlatformNotSupportedException();
#endif
            return null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the current content that is stored in the clipboard object.
        /// </summary>
        /// <returns>Contains the content of the Clipboard.</returns>
        public static DataPackageView GetContent()
        {
#if __ANDROID__
            if (_clipboardManager.HasPrimaryClip)
            {
                DataPackage package = new DataPackage();

                if (_clipboardManager.PrimaryClipDescription != null)
                {
                    package.Properties.Description = _clipboardManager.PrimaryClipDescription.Label;
                }

                ClipData data = _clipboardManager.PrimaryClip;
                for (int i = 0; i < data.ItemCount; i++)
                {
                    ClipData.Item item = data.GetItemAt(i);
                    if (!string.IsNullOrEmpty(item.Text))
                    {
                        package.SetText(item.Text);
                    }
                    else if (item.Uri != null)
                    {
                        package.SetWebLink(new Uri(item.Uri.ToString()));
                    }
                }

                return(package.GetView());
            }
#elif __IOS__
            if (UIPasteboard.General.Count > 0)
            {
                DataPackage package = new DataPackage();
                var         str     = UIPasteboard.General.String;
                if (!string.IsNullOrEmpty(str))
                {
                    package.SetText(str);
                }
                var url = UIPasteboard.General.Url;
                if (url != null)
                {
                    package.SetWebLink(new Uri(url.ToString()));
                }

                return(package.GetView());
            }
#elif __MAC__
            if (!string.IsNullOrEmpty(NSPasteboard.GeneralPasteboard.GetAvailableTypeFromArray(new string[] { NSPasteboard.NSPasteboardTypeString })))
            {
                DataPackage package = new DataPackage();
                string      str     = NSPasteboard.GeneralPasteboard.GetStringForType(NSPasteboard.NSPasteboardTypeString);
                if (!string.IsNullOrEmpty(str))
                {
                    package.SetText(str);
                }

                return(package.GetView());
            }
#elif WINDOWS_PHONE_APP
            if (_on10)
            {
                var dpv = _type10.GetRuntimeMethod("GetContent", new Type[0]).Invoke(null, new object[0]) as Windows.ApplicationModel.DataTransfer.DataPackageView;
                if (dpv != null)
                {
                    DataPackage package = new DataPackage();

                    if (!string.IsNullOrEmpty(dpv.Properties.Title))
                    {
                        package.Properties.Title = dpv.Properties.Title;
                    }
                    if (!string.IsNullOrEmpty(dpv.Properties.Description))
                    {
                        package.Properties.Description = dpv.Properties.Description;
                    }

                    foreach (string format in dpv.AvailableFormats)
                    {
                        if (format == StandardDataFormats.ApplicationLink || format == StandardDataFormats.WebLink || format == StandardDataFormats.Text)
                        {
                            Task <object> t = Task.Run <object>(async() =>
                            {
                                return(await dpv.GetDataAsync(format));
                            });
                            t.Wait();

                            package.SetData(format, t.Result);
                        }
                    }

                    return(package.GetView());
                }
            }
#elif WIN32
            string txt = GetText();
            if (!string.IsNullOrEmpty(txt))
            {
                DataPackage package = new DataPackage();
                package.SetText(txt);
                return(package.GetView());
            }
#else
            throw new PlatformNotSupportedException();
#endif
            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets the current content that is stored in the clipboard object.
        /// </summary>
        /// <param name="content">Contains the content of the clipboard.
        /// If NULL, the clipboard is emptied.</param>

        public async static void SetContent(DataPackage content)
        {
#if WINDOWS_PHONE_APP
            if (_on10)
            {
                _type10.GetRuntimeMethod("SetContent", new Type[] { typeof(Windows.ApplicationModel.DataTransfer.DataPackage) }).Invoke(null, new object[] { content });
                ContentChanged?.Invoke(null, null);
                return;
            }
#endif
            string text = "";
            Uri uri = null;

            if (content != null)
            {
                DataPackageView view = content.GetView();
                if (view.Contains(StandardDataFormats.Text))
                {

                    text = await view.GetTextAsync();
                }
                if (view.Contains(StandardDataFormats.WebLink))
                {
                    uri = await view.GetWebLinkAsync();
                }
                else if (view.Contains(StandardDataFormats.ApplicationLink))
                {
                    uri = await view.GetApplicationLinkAsync();
                }
            }
            else
            {
                Clear();
                return;
            }

#if __ANDROID__
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                _clipboardManager.PrimaryClip = ClipData.NewUri(null, content.Properties.Title, Android.Net.Uri.Parse(uri.ToString()));
            }
            else
            {
                _clipboardManager.PrimaryClip = ClipData.NewPlainText(content.Properties.Title, text);
            }

#elif __IOS__
            UIPasteboard.General.String = text;
            if(uri != null)
            {
                UIPasteboard.General.Url = NSUrl.FromString(uri.ToString());
            }
#elif __MAC__
            NSPasteboard.GeneralPasteboard.SetStringForType(text, NSPasteboard.NSPasteboardTypeString);

#elif WINDOWS_PHONE || WINDOWS_PHONE_APP
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                global::System.Windows.Clipboard.SetText(uri.ToString());
            }
            else
            {
                global::System.Windows.Clipboard.SetText(text);
            }
#elif WIN32
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                SetText(uri.ToString());
            }
            else
            {
                SetText(text);
            }
#else
            throw new PlatformNotSupportedException();
#endif
            ContentChanged?.Invoke(null, null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sets the current content that is stored in the clipboard object.
        /// </summary>
        /// <param name="content">Contains the content of the clipboard.
        /// If NULL, the clipboard is emptied.</param>

        public async static void SetContent(DataPackage content)
        {
#if WINDOWS_PHONE_APP
            if (_on10)
            {
                _type10.GetRuntimeMethod("SetContent", new Type[] { typeof(Windows.ApplicationModel.DataTransfer.DataPackage) }).Invoke(null, new object[] { content });
                ContentChanged?.Invoke(null, null);
                return;
            }
#endif
            string text = "";
            Uri    uri  = null;

            if (content != null)
            {
                DataPackageView view = content.GetView();
                if (view.Contains(StandardDataFormats.Text))
                {
                    text = await view.GetTextAsync();
                }
                if (view.Contains(StandardDataFormats.WebLink))
                {
                    uri = await view.GetWebLinkAsync();
                }
                else if (view.Contains(StandardDataFormats.ApplicationLink))
                {
                    uri = await view.GetApplicationLinkAsync();
                }
            }
            else
            {
                Clear();
                return;
            }

#if __ANDROID__
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                _clipboardManager.PrimaryClip = ClipData.NewUri(null, content.Properties.Title, Android.Net.Uri.Parse(uri.ToString()));
            }
            else
            {
                _clipboardManager.PrimaryClip = ClipData.NewPlainText(content.Properties.Title, text);
            }
#elif __IOS__
            UIPasteboard.General.String = text;
            if (uri != null)
            {
                UIPasteboard.General.Url = NSUrl.FromString(uri.ToString());
            }
#elif __MAC__
            NSPasteboard.GeneralPasteboard.SetStringForType(text, NSPasteboard.NSPasteboardTypeString);
#elif WINDOWS_PHONE || WINDOWS_PHONE_APP
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                global::System.Windows.Clipboard.SetText(uri.ToString());
            }
            else
            {
                global::System.Windows.Clipboard.SetText(text);
            }
#elif WIN32
            if (string.IsNullOrEmpty(text) && uri != null)
            {
                SetText(uri.ToString());
            }
            else
            {
                SetText(text);
            }
#else
            throw new PlatformNotSupportedException();
#endif
            ContentChanged?.Invoke(null, null);
        }