In the Prism framework, the UI communication between the main program and the module is realized.
Background:
The UI of the module contains the TreeView control. A check box is defined before each node of the tree control,
Public bool ShowCheckbox {get {return this. showCheckBox;} set {this. SetProperty (ref this. showCheckBox, value );}}
And add binding for it in View. The following is the binding of the Visibility attribute of CheckBox:
<UserControl.Resources> <HierarchicalDataTemplate x:Key="HierarchicalView" ItemsSource="{Binding SubCategories}"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"> <CheckBox IsChecked="{Binding IsChecked}" Margin="6,6,5,0" Visibility="{Binding ShowCheckbox,ElementName=treeViewControl,Converter={StaticResource BoolToVisibilityConverter}}" /> <TextBlock Text="{Binding Name}" FontSize="20" /> <StackPanel.ToolTip> <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" /> </StackPanel.ToolTip> </StackPanel> </HierarchicalDataTemplate></UserControl.Resources>
Then, we need to define an event in the underlying library Infrastructure. The main program communicates with the module through this event to modify the UI value of the module. The Code is as follows:
public class ControlVisibleEvent : PubSubEvent<bool> { }
Next, we subscribe and publish the event. Naturally, we subscribe to the above event in the module. The Code is as follows:
this.EventAggregator.GetEvent<DemoBase.ControlVisibleEvent>().Subscribe((value) => { this.ShowCheckbox = value; });
When subscribing to an event, we get the value and pass the obtained value to the ShowCheckbox attribute of ViewModel, and then use the WPF notification mechanism to achieve UI changes.
In the main program, we need to consider when to release the event. Here we choose to release the event completed by loading the module, the Code is as follows (note the highlighted code ):
public MainWindow(IEventAggregator eventAggregator, IModuleManager moduleManager) { InitializeComponent(); this.EventAggregator = eventAggregator; this.ModuleManager.LoadModuleCompleted += ModuleManager_LoadModuleCompleted; } void ModuleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e) { this.EventAggregator.GetEvent<DemoBase.ControlVisibleEvent>().Publish(false); }
In this case, in different applications, you only need to modify the value when releasing the event to achieve the UI change in the module.
Note:
More than the above methods are required to achieve this. In addition, using the RegionContext attribute of the Region object is also a good method. We can further study it later.