informasi kontak saya
Surat[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Perintah gabungan dapat memicu banyak perintah yang terdaftar di dalamnya, dan dapat menjalankan banyak perintah sekaligus. Persyaratan bisnis seperti menabung semua bisa tercapai.
1. Buat antarmuka ICompositeCommands dan kelas implementasi CompositeCommands
Gabungkan objek CompositeCommand yang disediakan di Prism ke dalam antarmuka ICompositeCommands dan kelas implementasi ApplicationCommands
public interface IApplicationCommands
{
CompositeCommand SaveCommand { get; }
}
public class ApplicationCommands : IApplicationCommands
{
private CompositeCommand _saveCommand = new CompositeCommand();
public CompositeCommand SaveCommand
{
get { return _saveCommand; }
}
}
2. Daftarkan perintah gabungan di IOC
Lakukan registrasi IOC dalam kode latar belakang App.xaml
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
}
3. Tentukan perintah gabungan di ViewModel
Tentukan perintah gabungan di ViewModel dan ikat perintah tersebut ke lapisan View
TampilanModel:
private IApplicationCommands _applicationCommands;
public IApplicationCommands ApplicationCommands
{
get { return _applicationCommands; }
set { SetProperty(ref _applicationCommands, value); }
}
public MainWindowViewModel(IApplicationCommands applicationCommands)
{
ApplicationCommands = applicationCommands;
}
Melihat:
<Button Content="Save" Margin="10" Command="{Binding ApplicationCommands.SaveCommand}"/>
4. Daftarkan perintah gabungan dengan Perintah di beberapa tempat lain.
public TabViewModel(IApplicationCommands applicationCommands)
{
_applicationCommands = applicationCommands;
UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);
_applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
}
private void Update()
{
UpdateText = $"Updated: {DateTime.Now}";
}
Dengan cara ini, ketika perintah gabungan ApplicationCommands.SaveCommand dipicu, semua perintah yang terdaftar akan dipicu.