Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1.  
  2. namespace NiHaoRS
  3. {
  4.    
  5.    
  6.     public class UnsupportedClipboard
  7.         : GenericClipboard
  8.  
  9.     {
  10.  
  11.         public UnsupportedClipboard()
  12.         { }
  13.    
  14.        
  15.         public override async System.Threading.Tasks.Task<string> GetClipboardContentAsync()
  16.         {
  17.             throw new System.NotSupportedException("UnsupportedClipboard.GetClipboardContentsAsync");
  18.         }
  19.  
  20.  
  21.         public override async System.Threading.Tasks.Task SetClipboardContentsAsync(string text)
  22.         {
  23.             throw new System.NotSupportedException("UnsupportedClipboard.SetClipboardContentsAsync");
  24.         }
  25.        
  26.        
  27.     }
  28.  
  29.  
  30.     public partial class OS
  31.     {
  32.         public static readonly GenericClipboard Clipboard;
  33.        
  34.        
  35.         static OS()
  36.         {
  37.             Clipboard = GenericClipboard.CreateInstance();
  38.         }
  39.  
  40.        
  41.        
  42.     }
  43.  
  44.  
  45.  
  46.     public abstract class GenericClipboard
  47.  
  48.     {
  49.        
  50.        
  51.         public abstract System.Threading.Tasks.Task<string> GetClipboardContentAsync();
  52.         public abstract System.Threading.Tasks.Task SetClipboardContentsAsync(string text);
  53.  
  54.  
  55.         public string Text
  56.         {
  57.             get
  58.             {
  59.                 string retValue = null;
  60.  
  61.                 Nito.AsyncEx.AsyncContext.Run(async delegate ()
  62.                 {
  63.                     retValue = await this.GetClipboardContentAsync();
  64.                 });
  65.  
  66.                 return retValue;
  67.             }
  68.  
  69.             set
  70.             {
  71.                 Nito.AsyncEx.AsyncContext.Run(async delegate ()
  72.                 {
  73.                     await this.SetClipboardContentsAsync(value);
  74.                 });
  75.  
  76.             }
  77.         }
  78.  
  79.  
  80.         public virtual string GetClipboardContents()
  81.         {
  82.             // System.Threading.Thread thread = System.Threading.Thread.CurrentThread;
  83.             // thread.IsBackground
  84.             // thread.IsThreadPoolThread
  85.             // System.Threading.SynchronizationContext.Current.
  86.            
  87.             return GetClipboardContentAsync().Result;
  88.         } // End Function GetClipboardContents
  89.        
  90.        
  91.         public virtual void SetClipboardContents(string text)
  92.         {
  93.             SetClipboardContentsAsync(text).Wait();
  94.         } // End Function GetClipboardContents
  95.        
  96.        
  97.         public static GenericClipboard CreateInstance()
  98.         {
  99.             // bool NativeEndianness = System.BitConverter.IsLittleEndian;
  100.            
  101.             if(System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
  102.                 return new WindowsClipboard();
  103.            
  104.             if(System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
  105.                 return new LinuxClipboard();
  106.            
  107.             if(System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
  108.                 return new OsxClipboard();
  109.            
  110.             return new UnsupportedClipboard();
  111.         }
  112.        
  113.        
  114.     }
  115.    
  116.  
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement