WPF: When is a TabItem actually loaded, rendered a

2019-07-21 14:50发布

In my WPF application, I have a class that serves as a wrapper around QuickTime. It provides all the specific or simplified functionality I need. To function, it needs to create an instance of QuickTime's ActiveX control and place it in a valid Windows Forms window. My app being WPF, the constructor works like this:

public VideoPlayerQT(WindowsFormsHost wfHost) {
    AxQTControl qtControl = new AxQTControl();
    wfHost.Child = qtControl;
}

Now in the main window, I use the player like this:

private VideoPlayerQT videoPlayer;    

private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
    this.videoPlayer = new VideoPlayerQT(myWinFormsHost);
}

This works until I put the WindowsFormsHost inside a TabControl. I want to have it on a tab that's not displayed right from the start.

This causes a strange behavior: The constructor of my VideoPlayerQT object tries to place the AxQTControl inside the provided WindowsFormsHost, Hwever, being on a not-yet-displayed tab, the QuickTime control throws InvalidActiveXStateException. I figure any ActiveX / COM control would throw that; I guess the WindowsFormsHost is in some "invalid ActiveX state" until its parent tab is clicked and displayed.

My question is: In which event handler (on which object) should construct the player? When is the WindowsFormsHost inside initially inactive TabItem as ready and loaded, as it is when Window_Loaded fires?

1条回答
Summer. ? 凉城
2楼-- · 2019-07-21 15:08

The first solution I came up with is creating the player in the method handling the TabItem_GotFocus event. Works fine for now, but if there is any catch to this solution, I'd like to know :)

Also, since GotFocus is such a general event I have trouble understanding why exactly is THIS okay for the WinForms host. Is the answer simply something like "tabitem's content is rendered when it receives focus, just as window's is rendered when it's loaded"?

查看更多
登录 后发表回答