ReactiveCollection和无效的跨线程访问

时间:2012-09-14 20:06:14

标签: windows-phone-7 reactiveui

我正在使用Reactive UI开发我的第一个Windows Phone 7.1应用程序,并且我在使用ReactiveCollection类时遇到了困难。

我的应用程序大致是关于访问WP7 SQL CE引擎(LINQ to SQL)。我想使用ReactiveAsyncCommand在后台执行数据操作。来自数据库的数据应该自动地#34;出现在UI中,因此我决定使用ReactiveCollection作为数据库和UI之间的代理。

这是我的模特,所以你可以有更好的主意:

public class EncounteredModel
{
    public ReactiveCollection<Fact> FactsCollection;

    public ReactiveCollection<FactEntry> FactEntriesCollection;

    private EncounteredModel()
    {
        using (EncounteredDataContext db = new EncounteredDataContext())
        {
            FactsCollection = new ReactiveCollection<Fact>(from fact in db.Facts select fact);
            FactEntriesCollection = new ReactiveCollection<FactEntry>(from factEntry in db.FactEntries select factEntry);
        }

        FactsCollection.ItemsAdded.Subscribe(fact => { using (var db = new EncounteredDataContext()) { db.Facts.InsertOnSubmit(fact); db.SubmitChanges(); } });
        FactsCollection.ItemsRemoved.Subscribe(fact => { using (var db = new EncounteredDataContext()) { db.Facts.DeleteOnSubmit(fact); db.SubmitChanges(); } });

        FactEntriesCollection.ItemsAdded.Subscribe(factEntry => { using (var db = new EncounteredDataContext()) { db.FactEntries.InsertOnSubmit(factEntry); db.SubmitChanges(); } });
        FactEntriesCollection.ItemsRemoved.Subscribe(factEntry => { using (var db = new EncounteredDataContext()) { db.FactEntries.DeleteOnSubmit(factEntry); db.SubmitChanges(); } });
    }

    private static EncounteredModel instance;

    public static EncounteredModel Instance
    {
        get 
        {
            if (instance == null)
                instance = new EncounteredModel();
            return instance; 
        }
    }

}

在我的视图模型中,我尝试过使用两种不同的变体:

  1. 创建派生的反应式集合并将UI绑定到它(使用ReactiveCollection.CreateDerivedCollection()方法。例如,它来自EncounteredModel.FactsCollection。
  2. 使用ObservableAsPropertyHelper<IEnumerable<Fact>>,然后订阅Model的ReactiveCollection Changed IObservable来填充OAPH。
  3. 不幸的是,这两种变体都给了我&#34;无效的跨线程访问。&#34;两种变体的堆栈跟踪通常是相同的,这里的变量1(缩写为重要部分):

       at MS.Internal.XcpImports.CheckThread()
       at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
       at System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)
       at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
       at System.Windows.Controls.ItemsControl.get_ItemsHost()
       at System.Windows.Controls.ItemsControl.OnItemsChangedHandler(Object sender, ItemsChangedEventArgs args)
       at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index, Boolean suppressEvent)
       at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
       at System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
    

    当我改为ReactiveCommand(不是异步的)时,一切都很好。任何想法如何克服这个?非常感谢提前。

0 个答案:

没有答案