2017-02-25 83 views
0

這裏是我試圖讓JS中的剪貼板文本:UWP:獲取剪貼板文本

function getClipboard() { 
    var content = Windows.ApplicationModel.DataTransfer.Clipboard.getContent(); 
    if (content.contains(Windows.System.string)) { 
     var text = content.getTextAsync(Windows.System.string); 
     $('section#show-clipboard #textarea').text(text); 
    } else {}; 
}; 

我使用的Clipboard.getContent()DataPackageView.getTextAsync(Windows.System.String)方法。

有趣的是,它並不重要,我是多麼的文本複製到剪貼板,它總是會說content.contains(Windows.System.string)等於false,並且將顯示content.getTextAsync(Windows.System.string);

一個例外是例外:

0x8004006a - JavaScript runtime error: Invalid clipboard format

DataPackage does not contain the specified format. Verify its presence using DataPackageView.Contains or DataPackageView.AvailableFormats. WinRT information: DataPackage does not contain the specified format. Verify its presence using DataPackageView.Contains or DataPackageView.AvailableFormats.

回答

2

我認爲Windows.System.string作爲參數是錯誤的嘗試content.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text)

+0

你是對的。至少DataPckageView傳遞if語句。但'JSON.stringify(content.getTextAsync(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text))'返回的對象是'{「operation」:{}}'。這沒有意義... – jonhue

+1

GetTextAsync是異步的,並返回一個JavaScript承諾,而不是實際的文本。有關如何執行,請參見https://docs.microsoft.com/en-us/windows/uwp/threading-async/asynchronous-programming-universal-windows-platform-apps#asynchronous-patterns-in-uwp-using-javascript它。 –