locked
Clipboard doesn't work

    Question

  • First, pasted my codes as usual:

            public static async Task<string> GetClipboardText()
            {
                DataPackageView dataPackageView = Clipboard.GetContent();
                if (dataPackageView.Contains(StandardDataFormats.Text))
                {
                    try
                    {
                        string text = await dataPackageView.GetTextAsync();
                        return text;
                    }
                    catch
                    { }
                }
                return "";
            }

    When I debugging these codes and step to line string text = await dataPackageView.GetTextAsync();, the app will pending there and no response any more, can anybody tell me what's going on.

    BTW, where is Clipboard.GetText()??

    Wednesday, May 21, 2014 3:10 PM

Answers

  • Hi William,

    Clipboard.GetText() is not a method can be used in Windows Store App, but you can use Clipboard.getContent() and then DataPackageView.getTextAsync() for getting the clipboard text.

    And please notice that this class is not agile, see the note of Clipborad class, probably you cannot use it in a Task thread:

    Note  : This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 2:06 AM
    Moderator
  • Hi William,

    I mean your code is correct, should be ok if you directly use it. But you should put them into a correct place as I mentioned this is not a agile class, otherwise your app will not continue and probably an exception will be thrown.

    Could you show me a demo that I can reproduce the issue? What I've done is to create a blank app and use button click to paste the clipboard text into a TextBox control. Nothing wrong.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 9:00 AM
    Moderator

All replies

  • Hi William,

    Clipboard.GetText() is not a method can be used in Windows Store App, but you can use Clipboard.getContent() and then DataPackageView.getTextAsync() for getting the clipboard text.

    And please notice that this class is not agile, see the note of Clipborad class, probably you cannot use it in a Task thread:

    Note  : This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 2:06 AM
    Moderator
  • Hi Jamles

    I just want to implement that paste the text in clipboard to my app, if my code doesn't work, can you give me a solution about it, that will be a big help for me.

    Thanks

    Thursday, May 22, 2014 8:00 AM
  • Hi William,

    I mean your code is correct, should be ok if you directly use it. But you should put them into a correct place as I mentioned this is not a agile class, otherwise your app will not continue and probably an exception will be thrown.

    Could you show me a demo that I can reproduce the issue? What I've done is to create a blank app and use button click to paste the clipboard text into a TextBox control. Nothing wrong.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 9:00 AM
    Moderator
  • Hi Jamles

    I have moved my code to a new class like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.ApplicationModel.DataTransfer;
    using Windows.Foundation.Metadata;
    
    namespace DataManage
    {
        [Threading(ThreadingModel.MTA)]
        [MarshalingBehavior(MarshalingType.Agile)]
        public class ClipboardAccess
        {
            public static void SetClipboardText(string text)
            {
                DataPackage package = new DataPackage();
                package.RequestedOperation = DataPackageOperation.Copy;
                package.SetText(text);
                Clipboard.SetContent(package);
            }
    
            public static async Task<string> GetClipboardText()
            {
                DataPackageView dataPackageView = Clipboard.GetContent();
                if (dataPackageView.Contains(StandardDataFormats.Text))
                {
                    try
                    {
                        string text = await dataPackageView.GetTextAsync();
                        return text;
                    }
                    catch
                    { }
                }
                return "";
            }
        }
    }
    

    Should I do like that? But it seems still doesn't work.

    Thursday, June 12, 2014 1:16 PM