Marshalling CoreDispatcher是否等同于win8应用商店应用程序中的WPF Dispatcher?
在WPF中使用Marshalling CoreDispatcher是否等同于win8应用商店应用程序中的WPF Dispatcher?,marshalling,observablecollection,winrt-xaml,windows-store-apps,Marshalling,Observablecollection,Winrt Xaml,Windows Store Apps,在WPF中使用ObservableCollection实现生产者-消费者模式时,我使用了封送处理技术来确保集合的事件在UI线程上调度,因为在工作线程上创建项目 在winrt中,我可以看到如何使用Dispatcher进行封送处理,如下所示: public void AddItem<T>(ObservableCollection<T> oc, T item) { if (Dispatcher.CheckAccess()) { oc.Add(it
ObservableCollection
实现生产者-消费者模式时,我使用了封送处理技术来确保集合的事件在UI线程上调度,因为在工作线程上创建项目
在winrt中,我可以看到如何使用Dispatcher
进行封送处理,如下所示:
public void AddItem<T>(ObservableCollection<T> oc, T item)
{
if (Dispatcher.CheckAccess())
{
oc.Add(item);
}
else
{
Dispatcher.Invoke(new Action(t => oc.Add(t)), DispatcherPriority.DataBind, item);
}
}
public async void AddItem<T>(ObservableCollection<T> oc, T item)
{
if (Dispatcher.HasThreadAccess)
{
oc.Add(item);
}
else
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { oc.Add(item); });
}
}
- 这是否正确使用了
李>CoreDispatcher
- 对于winrt中的基本并发生产者/消费者模式,有没有更好的方法来实现这一点
- 如果没有与
相同的静态访问器方法,我是否需要将Dispatcher
从UI向下传递到封送处理代码中corespatcher
- 以下是我所做的:
public Screen(IPreConfigurationService preConfigurationService, INavigationService navigationService)
{
_preConfigurationService = preConfigurationService;
_navigationService = navigationService;
if (!IsInDesignMode)
_currentDispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
}
public string UserMessage
{
get { return _userMessage; }
set
{
_userMessage = value;
SafelyRaisePropertyChanged("UserMessage");
}
}
protected void SafelyRaisePropertyChanged(string message)
{
if (!IsInDesignMode)
_currentDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => RaisePropertyChanged(message));
}
protected void ExecuteOnDispatcher(Action action)
{
_currentDispatcher.RunAsync(CoreDispatcherPriority.Normal, action.Invoke);
}
protected void SendUserMessage(string message)
{
UserMessage = message;
_currentDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => AlertOnError(message));
}