![](/img/trans.png)
[英]The calling thread must be STA, because many UI components require this in WPF
[英]The calling thread must be STA because many UI components requires this
System.Windows.Documents.Table MyTable= new System.Windows.Documents.Table(); System.Windows.Controls.Image BarcodeImg = new System.Windows.Controls.Image();
BarcodeImg.Source =BarcodeService.GetBarCode39AsImageSource(shippingLabelPrintModel.BarcodeData);
var Imageblock = new BlockUIContainer(BarcodeImg);
MyRow.Cells.Add(new System.Windows.Documents.TableCell(Imageblock) { TextAlignment = System.Windows.TextAlignment.Right });
大家好,這是我第一次使用 windows 服務,我正在嘗試將條形碼圖像添加到表格中,然后將表格添加到流文檔中。 但是,我收到錯誤“調用線程必須是 STA,因為許多 UI 組件都需要這個”。
我已經閱讀了解決方案是使用調度程序,任何人都可以幫助我如何使用調度程序在表內調用此圖像,因為我認為該線程已被另一個進程使用。
FYI UI 控件只能在 STA單線程單元線程上創建:
為避免這種情況,只需在最初創建控件的同一線程上訪問您的控件。
選項 1:
最快的方法是將[STAThread]
屬性放在 Main 方法上
例子:
class Program {
// Mark this attribute
[STAThread]
static void Main(string[] args) {
// rest of your code.
}
}
選項 2:
使用調度程序
Application.Current.Dispatcher.Invoke((Action)delegate{
// rest of your code for your bar code, no pun intended :)
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.