我試圖在我的應用程序中實現最小示例。在這個應用程序,我稱之爲營銷窗口(即ChromiumWebBrowser)以下方式: 我App.xaml.cs看起來是這樣的:CefSharp的最小示例和多線程
namespace ASA_Videowand
{
public partial class App
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// ...
foreach (List<string> myStrings in Screen.AllScreens.Select(myScreen => _xml.GetScreenConfigs(i)))
{
// ...
int i1 = i;
List<string> strings = myStrings;
var newWindowThread = new Thread(() =>
{
var myMarketing = new Marketing(i1, strings[0]);
myMarketing.Show();
Dispatcher.Run();
});
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
}
else
{
// ...
}
}
i++;
}
}
}
}
}
這是我Marketing.xaml和Marketing.xaml.cs:
<Window x:Class="ASA_Videowand.Marketing"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:main="clr-namespace:ASA_Videowand.Views"
Title="{Binding WebBrowser.Title}">
<Grid>
<main:MainView />
</Grid>
</Window>
和
namespace ASA_Videowand
{
public partial class Marketing
{
public Marketing(int screenNumber, string myLink)
{
InitializeComponent();
}
}
}
這裏說到我MainView.xaml:
<UserControl x:Class="ASA_Videowand.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
d:DesignWidth="640"
d:DesignHeight="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<cefSharp:ChromiumWebBrowser Grid.Row="0"
Address="http://www.google.com"
WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}"
Title="{Binding Title, Mode=TwoWay}" />
<StatusBar Grid.Row="1">
<ProgressBar HorizontalAlignment="Right"
IsIndeterminate="{Binding WebBrowser.IsLoading}"
Width="100"
Height="16"
Margin="3" />
<Separator />
<!-- TODO: Could show hover link URL here -->
<TextBlock />
</StatusBar>
</Grid>
</UserControl>
和我MainView.xaml.cs:
namespace ASA_Videowand.Views
{
public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
}
而且如此下去之前,它與exeption的MainView.xaml停止,即
<cefSharp:ChromiumWebBrowser Grid.Row="0"
Address="http://www.google.com"
WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}"
Title="{Binding Title, Mode=TwoWay}" />
將屬於另一個線程因此不能修改。 我是新的多線程。如果我用內置的IE-WebControl嘗試它,它可以很好地工作。我知道如何從代碼隱藏中調用某些東西,但在View中獲取這個異常對我來說是新的。 我該如何做這項工作?我很欣賞任何想法。謝謝。
莫非你的地方確切的錯誤呢? – Bono 2015-02-23 12:53:41
*我該怎麼做這項工作?* ...簡單...不要在後臺線程上做UI工作。 – Sheridan 2015-02-23 13:30:13
在德語中它的意思是:{「Der aufrufende線程可以用來處理對象,而不是用於對象的線程。」}英文翻譯應該是這樣的:「調用線程不能訪問這個對象,因爲對象由另一個線程擁有。「 – 2015-02-23 13:30:29