enum Power { On = 1, Off = 0, } class PowerEvent : PubSubEvent<Power> { } public enum SwitchConnection { Connection1, Connection2, } class SwitchEvent : PubSubEvent<SwitchConnection> { }
public class PowerViewModel : BindableBase { readonly IEventAggregator _aggregator; bool _power; public PowerViewModel(IEventAggregator aggregator) { _aggregator = aggregator; } public bool Power { get { return _power; } set { if (SetProperty(ref _power, value)) _aggregator.GetEvent<PowerEvent>().Publish(_power ? Events.Power.On : Events.Power.Off); } } }
public class SwitchViewModel : BindableBase { readonly IEventAggregator _aggregator; bool _switch; public SwitchViewModel(IEventAggregator aggregator) { _aggregator = aggregator; Switch = true; } public bool Switch { get { return _switch; } set { if (SetProperty(ref _switch, value)) _aggregator.GetEvent<SwitchEvent>().Publish(_switch ? SwitchConnection.Connection1 : SwitchConnection.Connection2); } } }
/// <summary> /// ViewModel . /// </summary> public class LedViewModel : BindableBase { readonly SwitchConnection _activeConnection; readonly Brush _activeLight; Power _currentPower; SwitchConnection _currentConnection; Brush _currentlight; public LedViewModel(SwitchConnection connection, Brush light, IEventAggregator aggregator) { _activeConnection = connection; _activeLight = light; aggregator.GetEvent<PowerEvent>().Subscribe(OnPowerChanged); aggregator.GetEvent<SwitchEvent>().Subscribe(OnSwitch); Update(); } /// <summary> /// . /// </summary> public Brush Light { get { return _currentlight; } private set { SetProperty(ref _currentlight, value); } } /// <summary> /// . /// </summary> void OnSwitch(SwitchConnection connection) { if (SetProperty(ref _currentConnection, connection)) Update(); } /// <summary> /// . /// </summary> void OnPowerChanged(Power power) { if (SetProperty(ref _currentPower, power)) Update(); } void Update() { Brush currentLight = Brushes.Transparent; switch (_currentPower) { case Power.On: if (_currentConnection == _activeConnection) currentLight = _activeLight; break; case Power.Off: break; default: throw new ArgumentOutOfRangeException(); } Light = currentLight; } }
<Window x:Class="AggregatorAntiPattern.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:AggregatorAntiPattern" mc:Ignorable="d" Height="350" Width="525"> <Window.Resources> <Style TargetType="Path" x:Key="Light"> <Setter Property="Stroke" Value="Black" /> <Setter Property="StrokeThickness" Value="2" /> <Setter Property="Fill" Value="{Binding Light}" /> <Setter Property="Data"> <Setter.Value> <EllipseGeometry RadiusX="10" RadiusY="10" /> </Setter.Value> </Setter> </Style> <Style TargetType="Line" x:Key="Connection"> <Setter Property="Stroke" Value="Black" /> <Setter Property="StrokeThickness" Value="1" /> </Style> </Window.Resources> <Canvas Margin="20"> <ToggleButton Canvas.Top="120" Content=" Power " DataContext="{Binding PowerVM}" IsChecked="{Binding Power}" /> <Line Canvas.Top="130" Canvas.Left="40" X1="0" X2="90" Y1="0" Y2="0" Style="{StaticResource Connection}" /> <ToggleButton Canvas.Top="120" Canvas.Left="120" Content=" Switch " DataContext="{Binding SwitchVM}" IsChecked="{Binding Switch}" /> <Line Canvas.Top="130" Canvas.Left="165" X1="0" X2="77" Y1="0" Y2="-30" Style="{StaticResource Connection}" /> <Line Canvas.Top="130" Canvas.Left="165" X1="0" X2="77" Y1="0" Y2="30" Style="{StaticResource Connection}" /> <Path Canvas.Top="100" Canvas.Left="250" DataContext="{Binding Connection1Light}" Style="{StaticResource Light}" /> <Path Canvas.Top="160" Canvas.Left="250" DataContext="{Binding Connection2Light}" Style="{StaticResource Light}" /> </Canvas> </Window>
var aggregator = new EventAggregator(); PowerVM = new PowerViewModel(aggregator); SwitchVM = new SwitchViewModel(aggregator); Connection1Light = new LedViewModel(SwitchConnection.Connection1, Brushes.Red, aggregator); Connection2Light = new LedViewModel(SwitchConnection.Connection2, Brushes.Blue, aggregator);
Source: https://habr.com/ru/post/322652/
All Articles