簡體   English   中英

STA線程-異常

[英]STA-Thread - Exception

我有一個具有打印作業的線程,該線程創建一個View,它是ViewModel並將其初始化。

沒有線程:

ViewModel具有一些綁定,並在將其提供給View后正確初始化。

//MyView : UserControl
//Constructor
DataContext = vm;
InitializeComponent();

用線程:

InitializeComponent()期間引發異常。

我已經在這里檢查了答案,並在Google上找到了答案,只發現了“通常”錯誤-未在STA中運行線程。 這似乎不適用於我的代碼。

//Join thread if valid
//set to null
PrintThread = new Thread(new ParameterizedThreadStart(ExecutePrintJob));
PrintThread.SetApartmentState(ApartmentState.STA);
PrintThread.Start(); //Takes parameter but unused at the moment


//Translated it
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: Line number "25" und Line position "43" from "An exception occured during the initialization of "System.Windows.Controls.Label.".


<Label  Content="TextBox --->"
        Grid.Column="0" Grid.Row="0"
        VerticalAlignment="Center" HorizontalAlignment="Center" />

在另一個線程(而非任務)中使用此方法時,有什么可能引發System.Windows.Markup.XamlParseException 我不確定如何解決這個問題,已經問了幾個人。

很好地了解用於打印的組件將使用UI組件“ System.Windows.Controls.Label”,因此應在主UI線程上實例化該組件。 我會嘗試整理該線程上的調用。 我遇到了類似的問題(即使在不同的情況下),我在這里發布了解決方案,希望對您有所幫助:

為什么即使我調用CommandManager.InvalidateRequerySuggested(),也不會調用WPF按鈕命令的CanExecute方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM