在WPF应用程序中,ObservableCollection由LINQ to SQL查询填充和更新.然后使用此ObservableCollection中的值更新UI对象.
通过LINQ to SQL查询更新此ObservableCollection的操作是否可能并且合理地在单独的线程中执行?
如果是,在这种情况下,它将是这个ObservableCollection的同一个实例吗?(我的意思是,如果从LINQ datacontext获取值并且为更新UI提供值的那个不同,那么我将无法更新UI)
我有一个看起来像这样的WPF代码.
public class AlphaProductesVM : BaseModel
{
private ObservableCollection<Alphabetical_list_of_product> _NwCustomers;
private int i = 0;
public AlphaProductesVM ()
{
_NwCustomers = new ObservableCollection<Alphabetical_list_of_product>();
var repository = new NorthwindRepository();
repository
.GetAllProducts()
.ObserveOn(SynchronizationContext.Current)
.Subscribe(AddElement);
}
public void AddElements(IEnumerable<Alphabetical_list_of_product> elements)
{
foreach (var alphabeticalListOfProduct in elements)
{
AddElement(alphabeticalListOfProduct);
}
}
public ObservableCollection<Alphabetical_list_of_product> NwCustomers
{
get { return _NwCustomers; }
set { _NwCustomers = value; }
}}
Run Code Online (Sandbox Code Playgroud)
我使用Unity来解决上述问题AlphaProductesVM.使用PRISM和UnityBootstrapper发现模块时,这是即时的.在运行时.ObserveOn(SynchronizationContext.Current)抛出异常并SynchronizationContext.Current在其中包含null值.