我试图从按钮调用选定的列表框项目,而不是 wpf 中的 listbox.selecteditemchanged
方法。所以当我尝试
string yadda = listbox.SelectedItem.ToString();
我得到一个异常(exception):
The calling thread cannot access this object because a different thread owns it.
所以,我想做的是:
Dispatcher.BeginInvoke(() =>
{
lbxSelectedItem = (lbxFileList.SelectedItem as TextBlock).Text;
});
这也不起作用,因为我得到另一个异常:
Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
最佳答案
将 lambda 转换为 Action
:
Dispatcher.BeginInvoke((Action)(() => { /*DoStuff*/ }));
或者从 lambda 构造一个:
Dispatcher.BeginInvoke(new Action(() => { /*DoStuff*/ }));
您可能可以为采用Action
的Dispatcher
编写一个扩展方法,这样lambda 就会被隐式转换。
关于c# - Dispatcher.BeginInvoke ,尝试使用 lambda 从文本 block 获取字符串集,但出现转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6181849/