在 WPF 中使用 Unity 解析时 SynchronizationContext.Current 为 null
我有一个 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
值。
I have a WPF Code which looks something like this.
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; }
}}
I use Unity to Resolve the above AlphaProductesVM
. This is instant when the Module is discovered using PRISM and the UnityBootstrapper. At runtime .ObserveOn(SynchronizationContext.Current)
throws an exception and SynchronizationContext.Current
has a null
value in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
SynchronizationContext.Current 属性将仅在主线程上调用时返回值。
如果您需要在线程中使用 SynchronizationContext 对象,主线程,您可以传递 SynchronizationContext 实例与主线程关联到需要它作为依赖项的类。
如果您选择此解决方案,您可以注册从SynchronizationContext.Current 属性在主线程上作为容器中的单例。这样,从那时起对 SynchronizationContext 的所有请求都将自动得到满足通过带有单例的容器:
The SynchronizationContext.Current property will only return a value when invoked on the main thread.
If you need to use a SynchronizationContext object in threads other than the main thread, you could pass the SynchronizationContext instance associated to the main thread to the classes that need it as a dependency.
If you choose this solution, you could register the SynchronizationContext object obtained from the SynchronizationContext.Current property on the main thread as a singleton in your container. That way all requests for a SynchronizationContext from that point on will automatically be satisfied by the container with the singleton:
尽管有 WPF 的
SynchronizationContext
实现,但不建议使用。 WPF 具有用于构建响应式应用程序的Dispatcher
。此外,仅当您位于 UI 线程上时,
SynchronizationContext.Current
才有值。如果您的逻辑在后台线程中运行,Current
将始终为 null。Although there is an implementation of
SynchronizationContext
for WPF it is not recommended for use. WPF has theDispatcher
to build responsive applications.In addition
SynchronizationContext.Current
only has a value if you are on the UI thread. If your logic runs in a background threadCurrent
will always be null.我不确定这是否会是一个受欢迎的建议,但您可以懒惰地创建并订阅您的收藏。然后,从 UI 线程第一次访问 NwCustomers 将正确启动一切。
或者,如果您将 UI 线程的调度程序注入到视图模型中,您可以在构造函数中订阅它。
I'm not sure if this will be a popular suggestion, but you could lazily create and subscribe to your collection. Then the first access to NwCustomers from the UI thread will kick everything off correctly.
or, if you inject the UI thread's dispatcher into your view model you can subscribe on that in the constructor.