c# - 如何使用单例类在 2 个视图模型之间发送 EventAggregator
问题描述
我尝试在 2 个视图模型之间使用 EventAggregator。但是我不知道如何创建单例类来连接 2 ViewModel
我参考这篇文章为我写一个例子
https://www.codeproject.com/Articles/812461/Event-Aggregator-Pattern
我的屏幕是当我单击列表视图上的每个项目时,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()。
解决方案
推荐阅读
- splunk - 从 splunk 中提取数据
- flutter - Flutter:在 list.generate 中制作列表项
- python - 破折号:输出('text2','children')类型错误:'输出'对象不可下标
- javascript - 如何基于prop在文件中动态导入react组件
- pytorch - 为什么 PyTorch 的内置损失函数只适用于 Long 张量类型?
- javascript - 使用 npm run-script build 构建应用程序后出现 useMemo 错误
- spring - 如何在一个控制器中接收多个RequestBody并在Spring Boot中根据其类型保存?
- python - 将 XLSX 转换为 CSV,而不会丢失公式中的值
- angular - Angular routerlink 使用分号而不是问号,url 的行为不符合预期
- c - 在 UDP 协议上使用 SRTP Cisco lib 从客户端发送数据时面临问题