I have one WPF Project. I want to display a TabControl, and its tabitem will be initialize dynamically, and tabitem's content is ChromiumWebBrowser. My main class is:
public partial class MainWindow : Window
{
public ChromiumWebBrowser browser;
public MainWindow()
{
InitializeComponent();
// init browser
browser = new ChromiumWebBrowser("https://test.vn");
RequestContextSettings requestContextSettings = new RequestContextSettings();
requestContextSettings.PersistSessionCookies = false;
requestContextSettings.PersistUserPreferences = false;
browser.Name = "B1";
browser.MinHeight = 0;
browser.RequestContext = new RequestContext(requestContextSettings);
browser.LoadingStateChanged += Browser_LoadingStateChanged;
}
private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
ChromiumWebBrowser tmpB = (ChromiumWebBrowser)sender;
Int16 tmpCount = Convert.ToInt16(tmpB.MinHeight);
}
private void DynamicTab_Loaded(object sender, RoutedEventArgs e)
{
TabItem tmpTI = new TabItem();
tmpTI.Content = this.browser;
tmpTI.Header = "TEST1";
this.dynamicTab.Items.Add(tmpTI);
}
and WPF xml code:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="Tools" Height="1040.761" Width="1743.478" WindowState="Maximized">
<Grid>
<Border Grid.Row="1" BorderBrush="Gray" BorderThickness="0,1">
<TabControl Name="dynamicTab" Loaded="DynamicTab_Loaded" SelectionChanged="DynamicTab_SelectionChanged">
</TabControl>
</Border>
</Grid>
</Window>
and the error was occurred at line Int16 tmpCount = Convert.ToInt16(tmpB.MinHeight);
. The error message is: The calling thread cannot access this object because a different thread owns it
.
Thank for your help!