我可以在 CLI 应用程序中使用 Clipboard.SetContent 函数吗?
Can I use the Clipboard.SetContent function in a CLI app?
函数执行无误 returns Ok(())
,但文本未被推入剪贴板:
pub fn copy_text(text_fragment: winrt::HString) -> winrt::Result<()> {
let data_package = DataPackage::new()?;
data_package.set_text(text_fragment)?;
Clipboard::set_content(data_package)
}
有关 Windows 运行时 API 的文档对 Clipboard.SetContent(DataPackage)
函数有以下声明:
Use this method after creating and defining a DataPackage
with the data you want to put on the clipboard. Call this method only when the application is in the foreground, or when a debugger is attached.
有什么方法可以在没有 UI 的情况下使用该功能?
我不知道它是否在 non-UI 线程上得到官方支持,但如果您按如下方式添加对 flush
的调用,它似乎可以工作:
use windows::application_model::data_transfer::*;
fn main() -> winrt::Result<()> {
let content = DataPackage::new()?;
content.set_text("hello world from Rust")?;
Clipboard::set_content(content)?;
Clipboard::flush()?;
Ok(())
}
flush
方法确保内容被复制到剪贴板上,并且即使发送 application/process 终止也会保留在那里。
函数执行无误 returns Ok(())
,但文本未被推入剪贴板:
pub fn copy_text(text_fragment: winrt::HString) -> winrt::Result<()> {
let data_package = DataPackage::new()?;
data_package.set_text(text_fragment)?;
Clipboard::set_content(data_package)
}
有关 Windows 运行时 API 的文档对 Clipboard.SetContent(DataPackage)
函数有以下声明:
Use this method after creating and defining a
DataPackage
with the data you want to put on the clipboard. Call this method only when the application is in the foreground, or when a debugger is attached.
有什么方法可以在没有 UI 的情况下使用该功能?
我不知道它是否在 non-UI 线程上得到官方支持,但如果您按如下方式添加对 flush
的调用,它似乎可以工作:
use windows::application_model::data_transfer::*;
fn main() -> winrt::Result<()> {
let content = DataPackage::new()?;
content.set_text("hello world from Rust")?;
Clipboard::set_content(content)?;
Clipboard::flush()?;
Ok(())
}
flush
方法确保内容被复制到剪贴板上,并且即使发送 application/process 终止也会保留在那里。