In the Prism framework, the UI communication between the main program and the module is realized.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.