//Runtime project,c class library using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace Runtime { public class DelCmd : ICommand { private Action<object> execute; private Predicate<object> canExecute; public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue) { execute = executeValue; canExecute = canExecuteValue; } public DelCmd(Action<object> executeValue):this(executeValue,null) { } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { if (parameter == null) { return true; } return canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Runtime { public static class Global { public static event Action<string> SendEvent; public static void OnSendEvent(string str) { SendEvent?.Invoke(str); } } }
//Module A <UserControl x:Class="ModuleA.ModuleAView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ModuleA" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.DataContext> <local:ModuelAViewModel/> </UserControl.DataContext> <Grid> <Button Content="Send Message" Command="{Binding SendCommand}" Width="200" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </UserControl> using Runtime; using System; using System.CodeDom; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ModuleA { public class ModuelAViewModel { public DelCmd SendCommand { get; set; } public ModuelAViewModel() { SendCommand = new DelCmd(SendCommandExecuted); } private void SendCommandExecuted(object obj) { string str=Guid.NewGuid().ToString(); Global.OnSendEvent(str); } } }
//Module B <UserControl x:Class="ModuleB.ModuleBView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ModuleB" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.DataContext> <local:ModuleBViewModel/> </UserControl.DataContext> <Grid> <ListBox ItemsSource="{Binding Messages}"/> </Grid> </UserControl> using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; using Runtime; namespace ModuleB { public class ModuleBViewModel { public ObservableCollection<string> Messages{ get; set; } public ModuleBViewModel() { Global.SendEvent += Global_SendEvent; Messages = new ObservableCollection<string>(); } private void Global_SendEvent(string obj) { Messages.Add(obj); } } }
//MainWindow <Window x:Class="WpfApp323.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:ma="clr-namespace:ModuleA;assembly=ModuleA" xmlns:mb="clr-namespace:ModuleB;assembly=ModuleB" xmlns:local="clr-namespace:WpfApp323" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ma:ModuleAView Grid.Column="0"/> <mb:ModuleBView Grid.Column="1"/> </Grid> </Window>
Global.OnSendEvent(str); public ModuleBViewModel() { Global.SendEvent += Global_SendEvent; Messages = new ObservableCollection<string>(); } private void Global_SendEvent(string obj) { Messages.Add(obj); }
标签:different,via,SendEvent,Global,communicate,System,using,Runtime,public From: https://www.cnblogs.com/Fred1987/p/18393129