[英]SynchronizationContext.Current is null on resolving with Unity in WPF
我有一个看起来像这样的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
中包含null
值。
SynchronizationContext.Current属性仅在主线程上调用时返回值 。
如果需要在主线程以外的线程中使用SynchronizationContext对象,则可以将与主线程关联的SynchronizationContext实例传递给需要它作为依赖项的类 。
如果选择此解决方案,则可以将从主线程上的SynchronizationContext.Current属性获取的SynchronizationContext对象注册为容器中的单例 。 这样,具有单例的容器将自动满足从该点开始的所有SynchronizationContext请求:
// Must run in the main thread
container.RegisterInstance(SynchronizationContext.Current);
虽然有一个针对WPF的SynchronizationContext
实现,但不建议使用它。 WPF具有Dispatcher
来构建响应式应用程序 。
此外,如果您位于UI线程上,则SynchronizationContext.Current
只有一个值。 如果您的逻辑在后台线程中运行,则Current
将始终为null。
我不确定这是否是一个受欢迎的建议,但你可以懒洋洋地创建和订阅你的收藏。 然后从UI线程第一次访问NwCustomers将正确地启动所有内容。
public AlphaProductesVM (){}
public ObservableCollection<Alphabetical_list_of_product> NwCustomers
{
get {
if(_NwCustomers == null)
{
_NwCustomers = new ObservableCollection<Alphabetical_list_of_product>();
var repository = new NorthwindRepository();
repository
.GetAllProducts()
.ObserveOn(SynchronizationContext.Current)
.Subscribe(AddElement);
}
return _NwCustomers;
}
}
或者,如果将UI线程的调度程序注入到视图模型中,则可以在构造函数中对其进行订阅。
var repository = new NorthwindRepository();
repository
.GetAllProducts()
.ObserveOn(theUIdispatcher)
.Subscribe(AddElement);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.