Is there a Clipboard plug-in/component for Xamarin.Forms?

JohnHardmanJohnHardman GBUniversity mod

I need to implement Copy to Clipboard within my app. Is there a component/plug-in to support this, or am I back to writing more DependencyServices?

Thanks,

John H.

Best Answers

  • JohnHardmanJohnHardman GB mod
    Accepted Answer

    @Ankur - Here is the code for WinPhone, Android and iOS. I haven't added Windows yet. Hopefully, it'll be the same as WinPhone, but I'm sometimes over-optimistic :-)

    For WinPhone:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        Clipboard.SetText(text);
                }
    
        } // public class ClipboardService : IClipboardService
    

    For Android:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        LaunchActivity.AndroidClipboardManager.Text = text;
                }
    
        } // public class ClipboardService : IClipboardService
    

    For iOS:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        UIPasteboard clipboard = UIPasteboard.General;
                        clipboard.String = text;
                }
    
        } // public class ClipboardService : IClipboardService
    
  • JohnHardmanJohnHardman GB mod
    Accepted Answer

    In the end, I replaced the above code by the Plugin.Share plugin - see https://github.com/jguertl/SharePlugin

Answers

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    What do you mean exactly by copy to clipboard?
    The operating system is managing this in the input controls.

  • JohnHardmanJohnHardman GBUniversity mod

    I have a page in my app that gathers together all of the information that is likely to be asked in a support situation (version numbers etc). I have a button on that page that when pressed, copies all of the information, in text form, to the clipboard.

    I've actually implemented it using DependencyServices now - it's just a few lines of code per platform - far simpler than expected. Just testing now.

  • AnkurAnkur USMember ✭✭

    @JohnHardman ,

    Can you share the code for this?
    Thanks!!!

  • JohnHardmanJohnHardman GBUniversity mod
    Accepted Answer

    @Ankur - Here is the code for WinPhone, Android and iOS. I haven't added Windows yet. Hopefully, it'll be the same as WinPhone, but I'm sometimes over-optimistic :-)

    For WinPhone:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        Clipboard.SetText(text);
                }
    
        } // public class ClipboardService : IClipboardService
    

    For Android:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        LaunchActivity.AndroidClipboardManager.Text = text;
                }
    
        } // public class ClipboardService : IClipboardService
    

    For iOS:

        public class ClipboardService : IClipboardService
        {
                public void CopyToClipboard(String text)
                {
                        UIPasteboard clipboard = UIPasteboard.General;
                        clipboard.String = text;
                }
    
        } // public class ClipboardService : IClipboardService
    
  • ElvinAsadovElvinAsadov AZMember ✭✭

    For Android it gives error when going to get access to LaunchActivity. Please give some additional information about this class. What is it?)

    Error 80 The name 'LaunchActivity' does not exist in the current context

  • JohnHardmanJohnHardman GBUniversity mod

    @ElvinAsadov - LaunchActivity is the class that is the main Android activity in my XF app, declared as follows:

        public class LaunchActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    

    AndroidClipboardManager is a property on that class, as follows:

        public static ClipboardManager AndroidClipboardManager { get; private set; }
    

    And that property is set in OnCreate, as follows:

        AndroidClipboardManager = (ClipboardManager)GetSystemService(ClipboardService);
    

    Hope that helps,

    John H.

  • ElvinAsadovElvinAsadov AZMember ✭✭
    edited September 2015

    @JohnHardman, Thanks for explanation.
    I have changed code for below on android:

    public class ClipboardService : IClipboardService
    {
        public void CopyToClipboard(String text)
        {
            // Get the Clipboard Manager
            var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
    
            // Create a new Clip
            ClipData clip = ClipData.NewPlainText("xxx_title", text);
    
            // Copy the text
            clipboardManager.PrimaryClip = clip;
        }
    }
    
  • JohnHardmanJohnHardman GBUniversity mod
    Accepted Answer

    In the end, I replaced the above code by the Plugin.Share plugin - see https://github.com/jguertl/SharePlugin

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2018

    @AlessandroCaliaro - I'm waiting until I hear something official about strategy for Caboodle before considering its use. I know it's open source, but are the Xamarin.Forms team officially supporting it and testing that it works against new releases of XF? Will it include support for Tizen, Mac etc.? Whilst I remain wary of using James Montemagno's plugins because of perceived key person risk (yes, I know the source is available so I could patch them if required), I don't see an advantage in moving to a replacement unless I know it's considered strategic, supports all Xamarin.Forms target platforms, and is kept up to date by Xamarin as required.

    cc @DavidOrtinau

Sign In or Register to comment.