我在实现System.ComponentModel.INotifyPropertyChanged的基类中有以下方法:
protected virtual void RaisePropertyChangedEventSynchronous(string propertyName)
{
try
{
if (PropertyChanged != null)
{
Delegate[] delegates = this.PropertyChanged.GetInvocationList();
foreach (PropertyChangedEventHandler handler in delegates)
{
try
{
DispatcherObject dispatcherInvoker = handler.Target
as DispatcherObject;
if (dispatcherInvoker != null)
{
dispatcherInvoker.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(delegate
{
handler(this, new PropertyChangedEventArgs(propertyName));
}));
}
else
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex,
ExceptionHandlingPolicyNames.LogPolicy);
}
}
}
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, ExceptionHandlingPolicyNames.LogPolicy);
}
}
Run Code Online (Sandbox Code Playgroud)
有时,我会将以下异常记录到文件中:
类型:System.InvalidOperationException,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089消息:暂停调度程序处理时无法执行此操作.来源:WindowsBase帮助链接:数据:System.Collections.ListDictionaryInternal TargetSite:Void PushFrame(System.Windows.Threading.DispatcherFrame)堆栈跟踪:位于System.Windows.Threading的System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架). System.Windows.Threading.Dispatcher.Invoke上的System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority优先级,TimeSpan超时,Delegate方法,Object args,Boolean isSingleParameter)中的DispatcherOperation.Wait(TimeSpan超时)(DispatcherPriority优先级,Delegate方法)在OCC600.Infrastructure.Dictionary.BusinessEntities.Observable.
如果我使用Dispatcher.BeginInvoke更新UI,我不会得到这些例外.但我发现使用BeginInvoke执行更新并不可靠,因为有时这些更改不会反映在用户界面上.
我该如何解决这个问题?
Joh*_*oty 10
我假设您在后台线程上并且正在尝试在UI线程上引发PropertyChanged事件.我认为WPF为您处理线程更改; 你不应该这样做.
这是我写的一些代码.XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding Value}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
C#:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
this.DataContext = new Backgrounder();
}
class Backgrounder : INotifyPropertyChanged {
int value = 0;
public Backgrounder() {
ThreadPool.QueueUserWorkItem(o => {
while (true) {
this.value++;
Notify("Value");
Thread.Sleep(TimeSpan.FromSeconds(1));
}
});
}
public int Value { get { return this.value; } }
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string property) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(property));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18904 次 |
| 最近记录: |