我遇到了InvalidOperationException异常,消息是“调用的线程必须是STA,因为很多UI组件都需要这个。”在一个WPF应用程序中,很大程度上依赖于引用的库。“调用线程必须是STA,因为很多UI组件都需要这个。” WPF
我试图确定错误来自哪里,使用各种线程和对象的调度程序,确保main()具有STAthread属性,尝试在看起来相关的方法上设置“[STAThread]”。
在MyParticipant构造函数中,由于正在构造MyVideoRenderer图片,它继承VideoRenderer,VideoRenderer构造函数本身抛出此异常,而不是进入构造函数。
代码:
public class MyParticipant : Participant //inside MainWindow.xaml.cs
{
public enum PictureMode
{
Avatar,
Video
}
public PictureMode pictureMode = PictureMode.Avatar;
public ProgressBar voiceVolume;
public Label nameLabel;
public MyVideoRenderer pic;
public MyVideo video;
public bool isCachedInClient = false;
public string displayName = null;
public Image avatarImage = null;
public static int picHeight = 480;
public static int piclWidth = 640;
public static int panelHeight = 155;
public static int panelWidth = 174;
public static Color liveColor = SystemColors.GradientActiveCaptionColor;
public static Color nonLiveColor = SystemColors.GradientInactiveCaptionColor;
public MyParticipant(uint objectId, VideoManager videoManager)
: base(objectId, videoManager)
{
pic = new MyVideoRenderer(videoManagerRef)
{
//Top = 5,
//Left = 5,
Height = picHeight,
Width = piclWidth,
//SizeMode = PictureBoxSizeMode.StretchImage
};
...
public class VideoRenderer : System.Windows.Controls.Image //referenced external class
{
public VideoRenderer(VideoManagerRoot videoManager) ///Exception here
{
this.videoManagerRef = videoManager;
}
...
你的问题是? – 2012-07-11 19:56:58
这段代码可能运行的线程完全不清楚。然而,该例外强烈表明它不在显示UI的主线程上。 – 2012-07-11 20:49:36