コード例 #1
0
        public static ClipData.Item AddAsClipDataItem(IMimeItem mimeItem)
        {
            var uri = NextItemUri;

            UriItems[uri] = mimeItem;
            return(new ClipData.Item(uri));
        }
コード例 #2
0
        public static string AsWindowsHtmlFragment(this IMimeItem mimeItem)
        {
            if (mimeItem.MimeType == "text/html" && mimeItem.Value is string html)
            {
                var start = html.Substring(0, Math.Min(html.Length, 300));
                if (!start.ToLower().Contains("<html>"))
                {
                    // we are going to assume we were given a fragment and need to encapsulate it for other Windows apps to recognize it (argh!)
                    var fragment = html;

                    var htmlStartIndex = 105;
                    var fragStartIndex = htmlStartIndex + 36;
                    var fragEndIndex   = fragStartIndex + fragment.Length;
                    var htmlEndIndex   = fragEndIndex + 36;

                    html  = "Version:0.9";
                    html += "\r\nStartHTML:" + htmlStartIndex.ToString("D10");
                    html += "\r\nEndHTML:" + htmlEndIndex.ToString("D10");
                    html += "\r\nStartFragment:" + fragStartIndex.ToString("D10");
                    html += "\r\nEndFragment:" + fragEndIndex.ToString("D10");
                    html += "\r\n<html>\r\n<body>\r\n<!--StartFragment-->";
                    html += fragment;
                    html += "<!--EndFragment-->\r\n</body>\r\n</html>";
                }
                return(html);
            }
            return(null);
        }
コード例 #3
0
        public static StorageFile ToStorageFile(this IMimeItem mimeItem)
        {
            StorageFile result = null;

            if (MimeSharp.Current.Extension(mimeItem.MimeType) is List <string> extensions && extensions.Count > 0 && extensions[0] is string ext)
            {
                var value = mimeItem.Value;
                if (value is string text)
                {
                    var fileName = Guid.NewGuid().ToString() + "." + ext;
                    var path     = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, fileName);
                    File.WriteAllText(path, text);
                    result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask());
                }
                else if (value is byte[] byteArray)
                {
                    var fileName = Guid.NewGuid().ToString() + "." + ext;
                    var path     = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, fileName);
                    File.WriteAllBytes(path, byteArray);
                    result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask());
                }
                else if (value is FileInfo fileInfo)
                {
                    var path = fileInfo.FullName;
                    result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask());
                }
            }
            return(result);
        }
コード例 #4
0
 public static KeyValuePair <NSString, NSObject> ToUiPasteboardItem(this IMimeItem mimeItem)
 {
     if (mimeItem.MimeType?.ToNsUti() is NSString nsUti) // && mimeItem.Value.ToNSObject() is NSObject nSObject)
     {
         NSData nsData = null;
         if (mimeItem.Value is byte[] byteArray)
         {
             nsData = NSData.FromArray(byteArray);
         }
         else if (mimeItem.Value is FileInfo fileInfo)
         {
             nsData = NSData.FromFile(fileInfo.FullName);
         }
         else if (mimeItem.Value is Uri uri)
         {
             var nsUrl = new NSUrl(uri.AbsoluteUri);
             nsData = NSData.FromUrl(nsUrl);
         }
         if (mimeItem.MimeType.StartsWith("image/", StringComparison.InvariantCultureIgnoreCase) && nsData != null && nsData.Length > 0)
         {
             var uiImage = UIImage.LoadFromData(nsData);
             return(new KeyValuePair <NSString, NSObject>(nsUti, uiImage));
         }
         NSObject nsObject = nsData ?? mimeItem.Value.ToNSObject();
         if (nsObject != null)
         {
             return(new KeyValuePair <NSString, NSObject>(nsUti, nsObject));
         }
     }
     return(new KeyValuePair <NSString, NSObject>());
 }
コード例 #5
0
 public static Android.Net.Uri AsAsAndroidUri(IMimeItem mimeItem)
 {
     Android.Net.Uri uri = null;
     if (mimeItem.Value != null)
     {
         uri           = NextItemUri;
         UriItems[uri] = mimeItem;
     }
     return(uri);
 }
コード例 #6
0
        public static NSDataItem Create(IMimeItem item)
        {
            var result = new NSDataItem(item);

            if (result.KeyedArchiver == null)
            {
                return(null);
            }
            return(result);
        }
コード例 #7
0
        private NSDataItem(IMimeItem item)
        {
            var nsObject = item.Value.ToNSObject();

            if (nsObject != null)
            {
                NSUti         = item.ToNsUti();
                KeyedArchiver = NSKeyedArchiver.ArchivedDataWithRootObject(nsObject);
            }
        }
コード例 #8
0
        public static string AsString(this IMimeItem mimeItem)
        {
            var value = mimeItem.Value;

            if (value is FileInfo fileInfo && fileInfo.Exists && fileInfo.Length > 0)
            {
                return(File.ReadAllText(fileInfo.FullName));
            }
            if (value is string text)
            {
                return(text);
            }
            return(null);
        }
コード例 #9
0
 public static NSString ToNsUti(this IMimeItem item)
 {
     return(item.MimeType.ToNsUti());
 }