我看到一个问题,即使用 Lync 2010 SDK 控件具有多个 UI 线程会导致程序崩溃。
我遇到的错误是:
{"Must create DependencySource on same Thread as the DependencyObject."}
如果有人可以解释为什么会发生这种情况,或者如何解决这个问题。这将会非常棒!
<Window x:Class="Lync.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lync="clr-namespace:Microsoft.Lync.Controls;assembly=Microsoft.Lync.Controls"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<lync:PresenceIndicator Source="sip:gawicks@home.com"/>
<Button Height="20" Width="20" Click="Button_Click"> </Button>
</StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
//Simply creating another UI thread on button click https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/
Thread thread = new Thread(() =>
{
MainWindow w = new MainWindow();
//Crash!!
w.Show();
w.Closed += (sender2, e2) =>
w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}