我有一个 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; }
}}
我使用Unity来解决上述问题AlphaProductesVM
。当使用 PRISM 和 UnityBootstrapper 发现模块时,这是即时的。运行时.ObserveOn(SynchronizationContext.Current)
抛出异常并且SynchronizationContext.Current
has a null
其中的价值。
The SynchronizationContext.Current财产仅在主线程上调用时才会返回值.
如果您需要使用同步上下文线程中的对象除了主线程之外,你可以通过同步上下文与主线程关联到需要它的类的实例作为依赖.
如果您选择此解决方案,您可以注册同步上下文从获得的对象SynchronizationContext.Current财产在主线程 as a 单例在你的容器中。这样所有的请求同步上下文从那时起,带有单例的容器将自动满足:
// Must run in the main thread
container.RegisterInstance(SynchronizationContext.Current);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)