Ars*_*han 5 c# wpf unity-container mvvm system.reactive
我有一个看起来像这样的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值.
SynchronizationContext.Current属性仅在主线程上调用时返回一个值。
如果需要在主线程以外的线程中使用SynchronizationContext对象,则可以将与主线程关联的SynchronizationContext实例传递给需要它作为依赖项的类。
如果选择此解决方案,则可以将从主线程上的SynchronizationContext.Current属性获取的SynchronizationContext对象注册为容器中的单例。这样,从那时起,所有对SynchronizationContext的请求都将自动由具有单例的容器满足:
// Must run in the main thread
container.RegisterInstance(SynchronizationContext.Current);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3464 次 |
| 最近记录: |