1 按照《Prsim项目搭建》搭建Prism项目
2 新建类库项目Prism.UseCompositeCommand.Core,通过nuget导入prism.unity,在该项目中添加类
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Prism.UseCompositeCommand.Core
{
public interface IApplicationCommands
{
CompositeCommand SaveCommand { get; }
}
public class ApplicationCommands : IApplicationCommands
{
private CompositeCommand _saveCommand = new CompositeCommand();
public CompositeCommand SaveCommand
{
get { return _saveCommand; }
}
}
}
3 新建用户控件类库ModuleA,通过nuget引入prism.unity
4 在ModuleA下面新建Views文件夹,并添加用户控件TabView
<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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModuleA.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<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>
5 在ModuleA下面新建ViewModels文件夹,并新建TabViewModel,在TabViewModel构造函数中注入IApplicationCommands
using Prism.Commands;
using Prism.Mvvm;
using Prism.UseCompositeCommand.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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}";
}
}
}
6 在ModuleA中新建类ModuleAEntity
using ModuleA.ViewModels;
using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA
{
public class ModuleAEntity : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
IRegion region = regionManager.Regions["ContentRegion"];
var tabA = containerProvider.Resolve<TabView>();
SetTitle(tabA, "Tab A");
region.Add(tabA);
var tabB = containerProvider.Resolve<TabView>();
SetTitle(tabB, "Tab B");
region.Add(tabB);
var tabC = containerProvider.Resolve<TabView>();
SetTitle(tabC, "Tab C");
region.Add(tabC);
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
void SetTitle(TabView tab, string title)
{
(tab.DataContext as TabViewModel).Title = title;
}
}
}
7 在App下面重写下面的方法
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<ModuleA.ModuleAModule>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
}
8 以上当某个Tab的Can Execute取消掉的话,即使切换到别的Tab Save也无效
9 这个可以让TabViewModel实现IActiveAware接口,它可以使选择未勾选Can Execute的Tab时候Save无效,而选择勾选Can Execute的Tab时候Save有效,但此时只相当于执行当前页的Save命令
using Prism;
using Prism.Commands;
using Prism.Mvvm;
using System;
using UsingCompositeCommands.Core;
namespace ModuleA.ViewModels
{
public class TabViewModel : BindableBase, IActiveAware
{
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}";
}
bool _isActive;
public bool IsActive
{
get { return _isActive; }
set
{
_isActive = value;
OnIsActiveChanged();
}
}
private void OnIsActiveChanged()
{
UpdateCommand.IsActive = IsActive;
IsActiveChanged?.Invoke(this, new EventArgs());
}
public event EventHandler IsActiveChanged;
}
}