Note that there are some explanatory texts on larger screens.

plurals
  1. POMust create DependencySource on same Thread as the DependencyObject
    text
    copied!<p>I bind observable dictionary from view model to view. I use Caliburn Micro Framework.</p> <p><strong>View:</strong></p> <pre><code> &lt;ListBox Name="Friends" SelectedIndex="{Binding Path=SelectedFriendsIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=SelectedFriend, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource friendsListStyle}" IsTextSearchEnabled="True" TextSearch.TextPath="Value.Nick" Grid.Row="2" Margin="4,4,4,4" PreviewMouseRightButtonUp="ListBox_PreviewMouseRightButtonUp" PreviewMouseRightButtonDown="ListBox_PreviewMouseRightButtonDown" MouseRightButtonDown="ListBox_MouseRightButtonDown" Micro:Message.Attach="[MouseDoubleClick]=[Action OpenChatScreen()]" &gt; </code></pre> <p><strong>Code from view model class.</strong></p> <p>Properties look like this:</p> <pre><code>public MyObservableDictionary&lt;string, UserInfo&gt; Friends { get { return _friends; } set { _friends = value; NotifyOfPropertyChange(() =&gt; Friends); } } </code></pre> <p>In Dispatcher timer I call every 3 seconds in seperate thread new service method.</p> <p>So I constructor of view model I have this:</p> <pre><code> _dispatcherTimer = new DispatcherTimer(); _dispatcherTimer.Tick += DispatcherTimer_Tick; _dispatcherTimer.Interval = TimeSpan.FromSeconds(3); _dispatcherTimer.Start(); _threadDispatcher = Dispatcher.CurrentDispatcher; </code></pre> <p>And Timer tick method is here:</p> <pre><code> private void DispatcherTimer_Tick(object sender, EventArgs eventArgs) { new System.Threading.Tasks.Task(() =&gt; { //get new data from server MyObservableDictionary&lt;string, UserInfo&gt; freshFriends = _service.GetFriends(Account); _threadDispatcher.BeginInvoke((System.Action)(() =&gt; { //clear data, Friend is property which is binded on listobox control Friends.Clear(); //here is problem - &gt; refresh data foreach (var freshFriend in freshFriends) { Friends.Add(freshFriend); } })); }).Start(); </code></pre> <p>when I run app I get this error:</p> <pre><code>Must create DependencySource on same Thread as the DependencyObject. at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri) at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren) at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) at System.Windows.FrameworkElement.ApplyTemplate() at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) </code></pre> <p>I try replace dispatcher:</p> <p>this <code>_threadDispatcher = Dispatcher.CurrentDispatcher;</code></p> <p>with this: <code>_threadDispatcher = Application.Current.Dispatcher;</code></p> <p>But it doesn’t help. Thank for advice.</p> <p>MyObservableDicionary is not dependency object or have dependecy property:</p> <pre><code>public class MyObservableDictionary&lt;TKey, TValue&gt; : IDictionary&lt;TKey, TValue&gt;, INotifyCollectionChanged, INotifyPropertyChanged {..} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload