【问题标题】:STA-Thread - ExceptionSTA 线程 - 异常
【发布时间】:2016-08-30 14:07:28
【问题描述】:

我有一个线程有一个打印作业,它创建一个视图,它是 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 组件需要您处于应用程序的 ui 线程中。如果您使用 Windows.Forms,请查看 Control 上的 Invoke 方法和 Dispatcher 上的 wpf

标签: c# wpf multithreading mvvm


【解决方案1】:

看看您用于打印的组件将使用作为 UI 组件的“System.Windows.Controls.Label”的异常,因此它应该在主 UI 线程上实例化。我会尝试在该线程上编组调用。我遇到了一个可能类似的问题(即使在不同的环境中),我在这里发布了我的解决方案,希望对您有所帮助:

Why WPF button's command's CanExecute method is not called even if I call CommandManager.InvalidateRequerySuggested()?

【讨论】:

    猜你喜欢
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多