Prism Demo系列(十二) Prism架构剖析:12-UsingCompositeCommands

一、简介

上篇博客,介绍了普通的命令绑定。这里介绍组合的命令绑定。当然项目中,不建议这种绑定,太复杂了,我整理了好久,没完全弄懂,以后还要回过头来看看CompositeCommand命令是什么鬼东西。

工程目录:

然后我们温习下,Prism的架构图:

 

参考:https://www.cnblogs.com/zhouyinhui/archive/2008/07/15/1243518.html

因为在这里,我们将更深入地讨论Prism架构图。

二、举例

工程目录:

2.1、工程项目UsingCompositeCommands

2.1.1、App.xaml、App.xaml.cs、MainWindow.xaml(与以前的系列相同)

2.1.2、Bootstrapper.cs:

using Microsoft.Practices.Unity;
using Prism.Unity;
using UsingCompositeCommands.Views;
using System.Windows;
using Prism.Modularity;
using UsingCompositeCommands.Core;

namespace UsingCompositeCommands
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

        /// <summary>
        /// 注册ApplicationCommands,让它的组合命令SaveCommand生效,以便给其他工程使用。
        /// </summary>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            
            Container.RegisterType<IApplicationCommands, ApplicationCommands>(new ContainerControlledLifetimeManager());
        }

        /// <summary>
        /// 利用ModuleCatalog,添加项目ModuleA的模块ModuleAModule.cs
        /// </summary>
        protected override void ConfigureModuleCatalog()
        {
            var catalog = (ModuleCatalog)ModuleCatalog;
            catalog.AddModule(typeof(ModuleA.ModuleAModule));
        }
    }
}

2.1.3、 MainWindowViewModel.cs:

using Prism.Mvvm;
using UsingCompositeCommands.Core;

namespace UsingCompositeCommands.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Unity Application";
        /// <summary>
        /// 更新MainWindow的Title,即Title="{Binding Title}
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private IApplicationCommands _applicationCommands;
        /// <summary>
        /// 更新MainWindow的命令ApplicationCommands.SaveCommand
        /// </summary>
        public IApplicationCommands ApplicationCommands
        {
            get { return _applicationCommands; }
            set { SetProperty(ref _applicationCommands, value); }
        }

        public MainWindowViewModel(IApplicationCommands applicationCommands)
        {
            ApplicationCommands = applicationCommands;
        }
    }
}

 2.2、UsingCompositeCommands.Core项目

2.2.1、ApplicationCommands.cs(实现CompositeCommand命令):

using Prism.Commands;

namespace UsingCompositeCommands.Core
{
    public interface IApplicationCommands
    {
        CompositeCommand SaveCommand { get; }
    }

    public class ApplicationCommands : IApplicationCommands
    {
        private CompositeCommand _saveCommand = new CompositeCommand();
        public CompositeCommand SaveCommand
        {
            get { return _saveCommand; }
        }
    }
}

2.3、ModuleA项目

2.3.1、ModuleAModule.cs:

using Microsoft.Practices.Unity;
using ModuleA.ViewModels;
using ModuleA.Views;
using Prism.Modularity;
using Prism.Regions;
using System;

namespace ModuleA
{
    public class ModuleAModule : IModule
    {
        IRegionManager _regionManager;
        IUnityContainer _container;

        public ModuleAModule(RegionManager regionManager, IUnityContainer container)
        {
            _regionManager = regionManager;
            _container = container;
        }

        public void Initialize()
        {
            IRegion region = _regionManager.Regions["ContentRegion"];

            var tabA = _container.Resolve<TabView>();
            SetTitle(tabA, "Tab A");
            region.Add(tabA);

            var tabB = _container.Resolve<TabView>();
            SetTitle(tabB, "Tab B");
            region.Add(tabB);

            var tabC = _container.Resolve<TabView>();
            SetTitle(tabC, "Tab C");
            region.Add(tabC);

        }

        /// <summary>
        /// 设置每个TabView的名称为Tab A或Tab B或Tab C
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="title"></param>
        void SetTitle(TabView tab, string title)
        {
            (tab.DataContext as TabViewModel).Title = title;
        }
    }
}

2.3.2、TabView.xaml(TabView.xaml.cs为默认):

<UserControl x:Class="ModuleA.Views.TabView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"             
             prism:ViewModelLocator.AutoWireViewModel="True">
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
        <TextBlock Text="{Binding Title}" FontSize="18" Margin="5"/>
        <CheckBox IsChecked="{Binding CanUpdate}" Content="Can Execute" Margin="5"/>
        <Button Command="{Binding UpdateCommand}" Content="Save" Margin="5"/>
        <TextBlock Text="{Binding UpdateText}"  Margin="5"/>
    </StackPanel>
</UserControl>

2.3.3、 TabViewModel.cs:

using Prism.Commands;
using Prism.Mvvm;
using System;
using UsingCompositeCommands.Core;

namespace ModuleA.ViewModels
{
    public class TabViewModel : BindableBase
    {
        IApplicationCommands _applicationCommands;

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private bool _canUpdate = true;
        public bool CanUpdate
        {
            get { return _canUpdate; }
            set { SetProperty(ref _canUpdate, value); }
        }

        private string _updatedText;
        public string UpdateText
        {
            get { return _updatedText; }
            set { SetProperty(ref _updatedText, value); }
        }

        public DelegateCommand UpdateCommand { get; private set; }

        public TabViewModel(IApplicationCommands applicationCommands)
        {
            _applicationCommands = applicationCommands;

            UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);

            _applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
        }

        private void Update()
        {
            UpdateText = $"Updated: {DateTime.Now}";
        }       
    }
}

2.4、运行效果:

2.4.1、点击上面的Save后:

 2.5、流程分析:

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/86540924