5、 How to browse prism sample code from GitHub to get started with eventaggregator in MVVM of prism under WPF

Time:2021-10-18

In this article, after reading examples 12 and 13, we write an example to execute applicationcommands under modules, and use iactiveaware to execute the commands of the current view or comands under applicationcommands. We mainly use view and region to decouple the relationship between them.

This article mainly looks at example 14, analyzes and learns the event aggregator.

Continue to learn the MVVM idea under prism from the 14 examples

1. Reference relation

The project includes four projects: ModuleA, moduleb, usingeventaggregator and usingeventaggregator.core.

1.1. ModuleA refers to prism.wpf and usingevnetaggregator.core;

1.2. Moduleb refers to prism.wpf and usingevventaggregator.core;

1.3. Usingeventaggregator.core refers to prism.core package;

1.4. The usingeventaggregator main project references prosm.unity, ModuleA, moduleb and usingeventaggregator.core;

We start with the smallest reference relationship

2. Usingeventaggregator.core project

The core project references prism.core

Contains only one messagesendevent.cs class

Messagesenevent inherits from pubsubevent. There is only this code under all cores.

Let’s go see other projects regardless of what he does.

3. ModuleA project

The ModuleA project references prism.wpf and usingeventaggregator.core

3.1 let’s first observe moduleamodule.cs

Moduleamodule inherits from prism.modularity.imodule and associates the display location relationship between messageview and display area leftregion in oninitialized() method.

3.2 observe messageview.xaml under views

The namespace prism is added. Prism: viewmodellocator. Autowireviewmodel = true is added to automatically associate viewmodels

In messageview.xaml, there is mainly a display control textbox for displaying message and a button control for executing sendmessagecommand command. There is no new content in the CS file

3.3 observe messageviewmodel.cs under viewmodels

In the messageviewmodel file, messageviewmodel inherits from bindablebase and adds the field of ieventaggregator event aggregator_ EA, assign the initial value to the object of ieventaggregator type passed in through the constructor,

The message attribute is created to bind to the textbox of the interface for display,

The delegatecommand command sendmessagecommand is created, and the SendMessage () method is initialized and bound in the constructor.

In the SendMessage () method, the calling field is called. Getevent of EA object().Publish(Message);

The whole is over. We don’t know what we’re doing here. It seems to be passed from to the ViewModel constructor_ EA object gets an event, and then publishes an object. The type of this object is defined in usingeventaggregator.core.

Forget the content of 12 13 examples and continue to look down.

4. Moduleb project

The moduleb project references prism.wpf and usingeventaggregator.core

4.1. First observe modulebmodule.cs at the entrance

Modulebmodule inherits from prism.modularity.imodule and associates the display location relationship between messagelist and display area rightregion in oninitialized() method.

4.2. Observe messagelist.xaml under views

Added namespace prism,

Added prism: viewmodellocator. Autowireviewmodel = true property for automatically associating viewmodels.

The list control for display is set and the itemsource is bound to the messages property under the VM. It’s strange that the region rgihtregion is not configured here, but they are irrelevant, so let’s ignore it first. Maybe in other places, prism has the advantage of displaying the contents of messages under ViewModel on the ListBox control. There is no additional code in the CS file.

4.3. Observe messagelistviewmodel.cs under viewmodels

Messagelistviewmodel inherits from bindablebase

The field ieventaggregator type is set_ EA, and EA is initialized in the constructor;

Set the messages object of observiblecollection type, and initialize messages in the constructor;

Use in constructors_ EA object.getevent(). Subscribe (messagereceived) to subscribe and receive messagesendevent messages published elsewhere, obtain the incoming parameters in the messagereceived () method and add them to messages.

Here is the message of receiving the messagesendevent of publish sent from other places. Then add it to your observablecollection to wait for the view to display.

5. Main project usingeventaggregator

Usingeventaggregator references prism.unity, ModuleA, moduleb and usingeventaggregator.core;

5.1. First look at app.xaml

Added namespace prism,

Modify the application to prism: prism: prism application,

The starturi attribute is removed

5.2、App.cs

The modified app inherits from prismapplication,

Overrides the createshell () method and sets the default startup form

Overriding the configuremodulecatalog () method and adding moduleamodule and modulembmodule.

5.3 mainwindow.xaml under views

Namespace prism is added and additional dependency property prism: viewmodellocator. Autowireviewmodel = true is set to associate ViewModel,

Two contentcontrol controls are added, and prism: regionmanager. Regionname = “leftregion” and prism: regionmanager. Regionname = “rightregion” are set to wait for view Association. No additional code in CS file

5.4 mainwindowviewmodel.cs under viewmodels

Mainwindowviewmodel inherits from bindablebase

Added the attribute title, which is used to display the title under the view

6 run the sample code and analyze it

After analyzing all the code, we run it

It is found that the messageview under ModuleA is on the left. After clicking button, the entered content will be displayed in the messagelist under moduleb on the right,

Recall

ModuleA associates the messageview view with the leftregion display area in the oninitialized () method of moduleamodule

Moduleb associates the messagelist view with the rightrion display area in the oninitialized () method of modulebmodule

Messagesendevent: pubsubevent is created in usingeventaggregator.core

In the constructor of the ViewModel of the ModuleA project, the EA object of ieventaggregator type is received, and the command triggered when the button in the view is clicked. Using this object, EA sends a string to the getevent.publish method.

In the ViewModel constructor of the moduleb project, the EA object of ieventaggregator type is received, and the getevent. Subscribe() method of the EA object is used in the ViewModel constructor to subscribe to the events sent in ModuleA. The whole process is over. Coupling is very low.

This article is finished. This article works better with iactiveaware in Article 13. This article is relatively simple. Don’t write a demo first, and continue to look back

I created a c# related communication group. For sharing learning materials and discussing problems. Welcome interested partners: QQ group: 542633085