C# 如何在WPF的ViewModel中使用Caliburn微事件聚合器

C# 如何在WPF的ViewModel中使用Caliburn微事件聚合器,c#,wpf,mvvm,eventaggregator,listcollectionview,C#,Wpf,Mvvm,Eventaggregator,Listcollectionview,在我的UserControl中,我使用了一个包含三个参数的ListCollectionView。其中两个使用个性化的RichTextBox解析HTML。在该控件上,集合中的下一项/上一项有两个按钮 我尝试在ViewModel中使用事件聚合器进行两项操作: 对于当前项,我需要在xml文件中写入一个参数 对于当前项,我需要重新初始化two RichTextBox的htmlformatter 第一个操作: public PopViewModel() { ...

在我的UserControl中,我使用了一个包含三个参数的ListCollectionView。其中两个使用个性化的RichTextBox解析HTML。在该控件上,集合中的下一项/上一项有两个按钮

我尝试在ViewModel中使用事件聚合器进行两项操作:

  • 对于当前项,我需要在xml文件中写入一个参数
  • 对于当前项,我需要重新初始化two RichTextBox的htmlformatter
  • 第一个操作:

        public PopViewModel()
        {
        ...
            AlertData.CollectionChanged += (s, e) => AlertCollectionView.Refresh();
            AlertCollectionView = new ListCollectionView(AlertData);
            AlertCollectionView.MoveCurrentToPosition(0);
    
        }
    
        public void PreviousRecordExecute()
        {
            AlertCollectionView.MoveCurrentToPrevious();
        }
    
        public void NextRecordExecute()
        {
            AlertCollectionView.MoveCurrentToNext();
        }
    
        private Alert CurrentAlert
        {
            get { return AlertCollectionView.CurrentItem as Alert; }
            set
            {
                AlertCollectionView.MoveCurrentTo(value);
                NotifyOfPropertyChange();
            }
        }
    
        public void MarkPopAlertAsRead(FeedItem CurrentAlert)
        {
            XElement doc = XElement.Load(diary);
            XElement elementToChange=doc.Descendants("Alert")
                .Single(x=>((int?)x.Element("key")==CurrentAlert.Alertid));
            elementToChange.SetElementValue("IsRead","True");
            doc.save(diary);
        }
    
    public partial class PopupView : UserControl
    {
        public PopupView()
        {
            InitializeComponent();
            this.AlertTitleRichTextBox.TextFormatter = new HtmlFormatter();
            this.AlertTxtRichTextBox.TextFormatter = new HtmlFormatter();
        }
    }
    
    MarkPopAlertAsRead是我需要与事件聚合器关联的方法

    第二个操作:

        public PopViewModel()
        {
        ...
            AlertData.CollectionChanged += (s, e) => AlertCollectionView.Refresh();
            AlertCollectionView = new ListCollectionView(AlertData);
            AlertCollectionView.MoveCurrentToPosition(0);
    
        }
    
        public void PreviousRecordExecute()
        {
            AlertCollectionView.MoveCurrentToPrevious();
        }
    
        public void NextRecordExecute()
        {
            AlertCollectionView.MoveCurrentToNext();
        }
    
        private Alert CurrentAlert
        {
            get { return AlertCollectionView.CurrentItem as Alert; }
            set
            {
                AlertCollectionView.MoveCurrentTo(value);
                NotifyOfPropertyChange();
            }
        }
    
        public void MarkPopAlertAsRead(FeedItem CurrentAlert)
        {
            XElement doc = XElement.Load(diary);
            XElement elementToChange=doc.Descendants("Alert")
                .Single(x=>((int?)x.Element("key")==CurrentAlert.Alertid));
            elementToChange.SetElementValue("IsRead","True");
            doc.save(diary);
        }
    
    public partial class PopupView : UserControl
    {
        public PopupView()
        {
            InitializeComponent();
            this.AlertTitleRichTextBox.TextFormatter = new HtmlFormatter();
            this.AlertTxtRichTextBox.TextFormatter = new HtmlFormatter();
        }
    }
    
    使用这段代码,如果我更改了当前项,这两个参数将从屏幕上消失,因此我需要重新加载Textformatter

    以及以下观点:

    <uc:RichTextBox Margin="29,86,31,301" x:Name="AlertTitleRichTextBox" Text="{Binding AlertCollectionView/AlertTitle, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>
        <TextBox Margin="257,378,10,12" Text="{Binding AlertCollectionView/AlertStartDate, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>
        <uc:RichTextBox Margin="10,113,10,48" x:Name="AlertTxtRichTextBox" Text="{Binding AlertCollectionView/AlertText, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>
    
    
    

    我的愿望是在集合更改时执行这两个操作(通过单击next/previous按钮)。我需要一些实施方面的帮助,因为Caliburn Micro文档对我没有帮助。

    通读一遍是的,我已经阅读了Caliburn关于聚合器的文档,并开始实施。但是,例如,我需要一个示例来了解如何侦听我以前的RecordExecute方法。有了这些文档,我不明白如何使用Handle方法无论你想做什么,上面的代码都不是mvvm,你能告诉我为什么吗?我可以在以后纠正这个问题,但就目前而言,我真的很想了解聚合器机制。