2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Une commande composée peut déclencher plusieurs commandes enregistrées avec elle et exécuter plusieurs commandes à la fois. Les exigences commerciales telles que la sauvegarde de tout peuvent être satisfaites.
1. Créez l'interface ICompositeCommands et la classe d'implémentation CompositeCommands
Enveloppez l'objet CompositeCommand fourni dans Prism dans l'interface ICompositeCommands et la classe d'implémentation ApplicationCommands
public interface IApplicationCommands
{
CompositeCommand SaveCommand { get; }
}
public class ApplicationCommands : IApplicationCommands
{
private CompositeCommand _saveCommand = new CompositeCommand();
public CompositeCommand SaveCommand
{
get { return _saveCommand; }
}
}
2. Enregistrez les commandes composées dans IOC
Effectuer l'enregistrement du CIO dans le code d'arrière-plan App.xaml
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
}
3. Définir les commandes composées dans ViewModel
Définir une commande composite dans ViewModel et lier la commande au calque View
Modèle de vue :
private IApplicationCommands _applicationCommands;
public IApplicationCommands ApplicationCommands
{
get { return _applicationCommands; }
set { SetProperty(ref _applicationCommands, value); }
}
public MainWindowViewModel(IApplicationCommands applicationCommands)
{
ApplicationCommands = applicationCommands;
}
Voir:
<Button Content="Save" Margin="10" Command="{Binding ApplicationCommands.SaveCommand}"/>
4. Enregistrez la commande composée avec les commandes à plusieurs autres endroits.
public TabViewModel(IApplicationCommands applicationCommands)
{
_applicationCommands = applicationCommands;
UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);
_applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
}
private void Update()
{
UpdateText = $"Updated: {DateTime.Now}";
}
De cette façon, lorsque la commande composée ApplicationCommands.SaveCommand est déclenchée, toutes les commandes enregistrées seront déclenchées.