使用来自另一个类的EventAggregator在Window的ViewModel中刷新属性

时间:2012-07-03 08:12:14

标签: c# .net wpf prism eventaggregator

所以..我有一些核心课程。在加载某些模块时,我试图通过EventAggregator.GetEvent()向状态窗口发送一些通知。发布()。 状态窗口显示:

private Core()
        {
            SystemManager.EventAggregator.GetEvent<StartProgressWindow>().Publish(null);
            this.InitializeCore();
        } 

其中StartProgressWindow正在启动status-window事件,而InitializeCore方法是所有模块正在加载的地方。

状态通知发送:

Core.EventAggregator.GetEvent<ChangeProgressStatus>().Publish("Some module is loading");

订阅者:

eventAggregator.GetEvent<ChangeProgressStatus>().Subscribe((message) =>
                {
                    this.Status = message;
                });

状态属性:

public string Status 
        {
            get
            {
                return status;
            }
            set
            {
                this.status = value;
                RaisePropertyChanged("Status");
            }
        }

结合:

<Label Content="{Binding Status, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

实际上,问题是:

属性更改了我发送给它的所有状态。但它没有反映在UI上,它是forfor。我在ViewModel类的构造函数中将Status-property的值设置为“Some string”,当然它反映得很好。 我做错了什么? 谢谢, 帕维尔!

2 个答案:

答案 0 :(得分:0)

您是否尝试过确定是否订阅了UI线程?

eventAggregator.GetEvent<ChangeProgressStatus>().Subscribe((message) =>
                {
                    this.Status = message;
                }, ThreadOption.UIThread);

答案 1 :(得分:0)

我怀疑是因为所有事件都在InitializeCore()方法的同时处理,因此在所有事件和InitializeCore()方法完成之前它实际上不会呈现任何内容

尝试在较低DispatcherPriority然后StartProgressWindow启动Render事件,例如DispatcherPriority.Background

private Core()
{
    this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new Action(delegate 
        { 
            SystemManager.EventAggregator.GetEvent<StartProgressWindow>().Publish(null);
        }));

    this.InitializeCore();
}