Do I need EventAggregator here?

I’m learning MVVM pattern with C#, WPF and .NET Framework 4.5.1.

I’m using MVVM Light framework to do it and now I have a doubt. Reading the book Windows 8 MVVM Patterns Revealed I see that the author uses Event Aggregator Pattern.

I’m not sure if I need it, because in the following ViewModel I would have to raise the event to EventAggregator and handle it on the same ViewModel.

Here is my ViewModel:

public class MainViewModel : ViewModelBase
{
    private RelayCommand doLoginCommand;

    /// <summary>
    /// The <see cref="UserName" /> property's name.
    /// </summary>
    public const string UserNamePropertyName = "UserName";

    private string _userName = null;

    /// <summary>
    /// Sets and gets the UserName property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string UserName
    {
        get
        {
            return _userName;
        }

        set
        {
            if (_userName == value)
            {
                return;
            }

            RaisePropertyChanging(UserNamePropertyName);
            _userName = value;
            RaisePropertyChanged(UserNamePropertyName);
        }
    }

    /// <summary>
    /// The <see cref="UserPassword" /> property's name.
    /// </summary>
    public const string UserPasswordPropertyName = "UserPassword";

    private string _userPassword = null;

    /// <summary>
    /// Sets and gets the UserPassword property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string UserPassword
    {
        get
        {
            return _userPassword;
        }

        set
        {
            if (_userPassword == value)
            {
                return;
            }

            RaisePropertyChanging(UserPasswordPropertyName);
            _userPassword = value;
            RaisePropertyChanged(UserPasswordPropertyName);
        }
    }

    public RelayCommand DoLoginCommand
    {
        get { return doLoginCommand; }
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        ////if (IsInDesignMode)
        ////{
        ////    // Code runs in Blend --> create design time data.
        ////}
        ////else
        ////{
        ////    // Code runs "for real"
        ////}

        this.doLoginCommand = new RelayCommand(ExecuteDoLogin);
    }

    private void ExecuteDoLogin()
    {
        Debug.WriteLine(_userName);
    }
}

And its XAML:

<Window x:Class="WPFMvvmApress.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">
    <Grid>
        <StackPanel x:Name="loginPanel" HorizontalAlignment="Center" Height="159" Margin="0" VerticalAlignment="Center" Width="349">
            <TextBox 
                x:Name="userName" 
                HorizontalAlignment="Left" 
                Height="23" 
                TextWrapping="Wrap" 
                VerticalAlignment="Top" 
                Width="231" 
                Margin="10,10,0,5" 
                Text="{Binding Path=UserName, Mode=TwoWay}"/>
            <TextBox 
                x:Name="userPassword" 
                HorizontalAlignment="Left" 
                Height="23" 
                TextWrapping="Wrap" 
                VerticalAlignment="Top" 
                Width="231" 
                Margin="10,5,0,5"/>
            <Button 
                x:Name="loginButton" 
                Content="Login" 
                HorizontalAlignment="Left" 
                VerticalAlignment="Top" 
                Width="75" 
                Margin="10,5" 
                Command="{Binding Path=DoLoginCommand}"/>
        </StackPanel>

    </Grid>
</Window>

What do you think?

4

The general idea of MVVM is to use the ViewModel to model the View so that the ViewModel data-binds to elements on the View which usually includes the Model (for more information, see my answer to this question: How do I know if I’m violating MVVM with WPF?).

The event aggregator or mediator is used to transmit events between the layers that should not be aware of each other. In your example above, data-binding to a command on the ViewModel is the preferred way to handle events that can be bound to a command.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật