Using the prism third-party framework for communication between ViewModel
Create class inherits from Unitybootstrapper
public class Bootstrapper:unitybootstrapper
{
protected override System.Windows.DependencyObject Createshell ()
{
MainWindow shell = container.resolve<mainwindow> ();
Shell. Show ();
return shell;
}
}
Custom events
Class userchangedevent:compositepresentationevent<clsdatamanage.wordtest>
{
}
Setting the startup page in the App.xaml.cs file
protected override void Onstartup (StartupEventArgs e)
{
Base. Onstartup (e);
var bootstrapper = new Bootstrapper ();
Bootstrapper. Run ();
}
Registering events in ViewModel
public class Wordmanageviewmodel
{
protected Ieventaggregator Eventaggregator;
Public Wordmanageviewmodel ()
{
Design-time State judgment
if (System.Diagnostics.Process.GetCurrentProcess (). ProcessName = = "Devenv")
{
Return
}
Get event Aggregator
This.eventaggregator = servicelocator.current.getinstance<ieventaggregator> (ClsDataManage.wordtest);
}
public void Insert (object name)
{
Publish Events
Base.eventaggregator.getevent<userchangedevent> (). Publish (wordtest);
}
Receive event handling in another ViewModel
public class Testmanageviewmodel
{
protected Ieventaggregator Eventaggregatorex;
protected Subscriptiontoken token;
Public Testmanageviewmodel ()
{
if (System.Diagnostics.Process.GetCurrentProcess (). ProcessName = = "Devenv")
{
Return
}
Subscribe to Events
Eventaggregatorex = servicelocator.current.getinstance<ieventaggregator> ();
token = eventaggregatorex.getevent<userchangedevent> (). Subscribe (userchanged);
}
Event triggering
public void userchanged (Clsdatamanage.wordtest wordtest)
{
Perform Action update data
End performing Operation Update data
if (token! = null)
{
Eventaggregatorex.getevent<userchangedevent> (). Unsubscribe (userchanged);
}
}
}
Communication between WPF ViewModel