Created
January 19, 2018 07:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IClipboardService | |
{ | |
string GetTextFromClipboard(); | |
void SendTextToClipboard(string text); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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