Skip to content

Instantly share code, notes, and snippets.

@yuv4ik
Created January 19, 2018 07:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuv4ik/56e15e3d65eae4efc60af0a14942d7cd to your computer and use it in GitHub Desktop.
Save yuv4ik/56e15e3d65eae4efc60af0a14942d7cd to your computer and use it in GitHub Desktop.
[Xamarin.Forms] [iOS] [Droid] IClipboardService to read and write text value from the Clipboard
public class ClipboardService : IClipboardService
{
public string GetTextFromClipboard()
{
var clipboardmanager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
var item = clipboardmanager.PrimaryClip.GetItemAt(0);
var text = item.Text;
return text;
}
public void SendTextToClipboard(string text)
{
// Get the Clipboard Manager
var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
// Create a new Clip
var clip = ClipData.NewPlainText("YOUR_TITLE_HERE", text);
// Copy the text
clipboardManager.PrimaryClip = clip;
}
}
public interface IClipboardService
{
string GetTextFromClipboard();
void SendTextToClipboard(string text);
}
public class ClipboardService : IClipboardService
{
public string GetTextFromClipboard() => UIPasteboard.General.String;
public void SendTextToClipboard(string text) => UIPasteboard.General.String = text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment