這是我的第一個問題。我開始使用XAML和C#,希望我能解釋我的問題並獲得一些幫助。 恐怕我的方法不可能實現,但也許我可以得到一些解決方法的幫助。C#XAML在BackGroundWorker中創建StackPanel
我需要打印大量的卡片。該進程正在運行但凍結了主線程。我想在BackgroundWorker內部進行這種批量打印,並在主線程中報告進度。 我發現了一些如何做的例子,並且我創建了運行backgroundworker所需的所有東西。
問題在於,對於每張卡片,我創建一個用於構建圖片的堆疊面板,最後使用FixedDocument和printdialog進行打印。
代碼崩潰的類accreditation_graphic的第一行
void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
ObservableCollection<accreditation_field> _acc_list;
_acc_list = _param_acc_list;
accreditation_graphic _graphic = new accreditation_graphic();
// The sender is the BackgroundWorker object we need it to
// report progress and check for cancellation.
//NOTE : Never play with the UI thread here...
foreach (accreditation_field _acc in _acc_list)
{ [...] }
[...]
}
類是:
class accreditation_graphic
{
private StackPanel _stack = new StackPanel();
public accreditation_graphic()
{
}
[...]
和螺紋在沒有錯誤完成:私人StackPanel _stack =新的StackPanel();
有沒有辦法在backgroundworkers中創建一個stackpanel?
謝謝。
請參閱[此問題](http://stackoverflow.com/q/2763323/1136211)。但是,如果您將其設置爲STA(單線程單元),則可以在不同的線程中創建UI元素。但是它不會(很容易)在UI線程中使用這些元素。他們完全分開。 – Clemens