Iman
Iman

Reputation: 145

Can't get contents of the Clipboard in Windows 8.1 app

I am using the Clipboard class in a Windows 8.1 app to get the contents of the clipboard as it changes. But when I try to write the contents into a text box I get this:

clipboard now contains: System.__ComObject

here is my code:

private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            Clipboard.ContentChanged += Clipboard_ContentChanged;
        }

        private void Clipboard_ContentChanged(object sender, object e)
        {
            textBox.Text = "clipboard now contains: " + Clipboard.GetContent().GetTextAsync();
        }

I want to get the string that is copied to the clipboard using GetContent().GetTextAsync() methods but I don't know why it returns System.__ComObject. Thank you in advance

Upvotes: 0

Views: 155

Answers (1)

Bas
Bas

Reputation: 2038

GetTextAsync() is an async method, and as such, must be awaited. If you don't await it, you'll just get an instance of IAsyncOperation, since that's its return type.

You need to read up on async/await to learn the details (this is a good place to start), but to answer your question, change your event handler to the following:

private async void Clipboard_ContentChanged(object sender, object e)
{
    textBox.Text = "clipboard now contains: " + await Clipboard.GetContent().GetTextAsync();
}

Note the async keyword in the method declaration to signify that this is an async method (you can only await async methods from within async methods), and the await keyword that, simply put, converts the IAsyncOperation to a string value when the operation is complete.

Upvotes: 3

Related Questions