ICommand & IoC

main
Yik Teng Hie 3 years ago
parent 3fe81c9581
commit 40d86a807e

@ -194,4 +194,74 @@ namespace MitechLib.Modules.PackagePage.Views
{"CarrierInfoView", "MitechLib.Modules.PackagePage.Views.CarrierInfoView"}
};
```
## Guide to link View command to ViewModel
1. Create `ICommand` variable in view model. Naming convention `<Action>Command`
2. Create `DelegateCommand` to the ICommand within the constructor
3. Create the Delegate Command callback function
```cs
[Export]
public class CarrierInfoViewModel : ViewModelBase, IHeaderInfoProvider<string>
{
// Step1
public ICommand UndoCommand { get; set; }
[ImportingConstructor]
public CarrierInfoViewModel(IRegionManager _regionManager)
{
// Step2: Initialize ICommand delegate
this.UndoCommand = new DelegateCommand(this.OnUndoCommand);
}
// Step3: Callback
private void OnUndoCommand()
{
ConfirmMessageBoxView confirmmessagebox = new ConfirmMessageBoxView("Confirm to undo changes?");
confirmmessagebox.ShowDialog();
if (confirmmessagebox.DialogResult == false)
{
return;
}
// TODO: Add Business logic HERE
//UpdateWaferInfo(selectedPackage);
}
}
```
4. Bind the ICommand to View xaml
```xml
<Button Content="Undo" Command="{Binding UndoCommand}"/>
```
## Guide for ViewModel IoC via constructor injection
1. Attach [ImportingConstructor] to constructor
2. Assign to a field
3. Use the object within the class
```cs
[Export]
public class CarrierInfoViewModel : ViewModelBase, IHeaderInfoProvider<string>
{
// Step1
public IRegionManager RegionManager { get; }
// Step2
[ImportingConstructor]
public CarrierInfoViewModel(IRegionManager _regionManager)
{
//
RegionManager = _regionManager;
}
// Step3
private void OnExitCommand()
{
RegionManager.RequestNavigate(RegionNames.PackageMainViewRegion, new Uri("PackageMgmtView", UriKind.Relative));
}
}
```
Loading…
Cancel
Save