首页 > 解决方案 > 如何使用单例类在 2 个视图模型之间发送 EventAggregator

问题描述

我尝试在 2 个视图模型之间使用 EventAggregator。但是我不知道如何创建单例类来连接 2 ViewModel

我参考这篇文章为我写一个例子

https://www.codeproject.com/Articles/812461/Event-Aggregator-Pattern

https://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

我的屏幕是当我单击列表视图上的每个项目时,detailListView 显示详细项目

这是我的代码

物品.cs

public class Item
{
    public Item() {}
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
}

列表视图模型.cs

internal class ListViewModel
{
    protected readonly IEventAggregator _eventAggregator;
    public ListViewModel(IEventAggregator eventAggregator)
    {
        this._eventAggregator = eventAggregator;

        this.Items = new List<Item>();
        this.Items.Add(new Item { Id = 1, Name = "Item A", Price = 100.00, Quantity = 250 });
        this.Items.Add(new Item { Id = 2, Name = "Item B", Price = 150.00, Quantity = 150 });
        this.Items.Add(new Item { Id = 2, Name = "Item C", Price = 300.00, Quantity = 100 });
    }

    public List<Item> Items { get; private set; }

    private Item _selectedItem;
    public Item SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;

            //Publish event:
            this._eventAggregator.PublishEvent(_selectedItem);
        }
    }
}

详细信息ViewModel.cs

public class DetailsViewModel : INotifyPropertyChanged
{
    protected readonly IEventAggregator _eventAggregator;
    public DetailsViewModel(IEventAggregator eventAggregator)
    {
        this._eventAggregator = eventAggregator;
        _eventAggregator.SubsribeEvent(this);
    }

    private Item _item;
    public Item Item
    {
        get { return _item; }
        set { _item = value; NotifyPropertyChanged("Item"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

列表视图.xaml

<Grid>
    <ListBox ItemsSource="{Binding Items}"
             SelectedItem="{Binding SelectedItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

ListView.xaml.cs

public partial class ListView : UserControl
{
    public ListView()
    {
        InitializeComponent();
        DataContext = new ListViewModel(ApplicationService.Instance.EventAggregator);
    }
}

详细信息视图.xaml

<Grid>
    <ContentControl Content="{Binding Item}">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Id: " FontWeight="Bold" Grid.Row="0"/>
                    <TextBlock Text="Name: " FontWeight="Bold" Grid.Row="1"/>
                    <TextBlock Text="Price: " FontWeight="Bold" Grid.Row="2"/>
                    <TextBlock Text="Quantity: " FontWeight="Bold" Grid.Row="3"/>
                    <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="1"/>
                    <TextBlock Text="{Binding Name}" Grid.Row="1" Grid.Column="1"/>
                    <TextBlock Text="{Binding Price, StringFormat=c2}" Grid.Row="2" Grid.Column="1"/>
                    <TextBlock Text="{Binding Quantity}" Grid.Row="3" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
</Grid>

详细信息视图.xaml.cs

public partial class DetailsView : UserControl
{
    public DetailsView()
    {
        InitializeComponent();
        DataContext = new DetailsViewModel(ApplicationService.Instance.EventAggregator);
    }
}

和finnaly单例类连接2个ViewModel

应用服务.cs

internal sealed class ApplicationService
{
    private ApplicationService() { }

    private static readonly ApplicationService _instance = new ApplicationService();

    internal static ApplicationService Instance { get { return _instance; } }

    private IEventAggregator _eventAggregator;
    internal IEventAggregator EventAggregator
    {
        get
        {
            if (_eventAggregator == null)
                _eventAggregator = new EventAggregator();

            return _eventAggregator;
        }
    }
}

我不知道如何创建 _eventAggregator = new EventAggregator(); Gautham Prabhu K 的文章中没有 EventAggregator()。

标签: c#eventaggregator

解决方案


推荐阅读