# Adding New View-ViewModel for Package Info
1. Create XAML view code
```xml
```
2. Create C# ViewModel Class
```cs
using MitechLib.Infrastructure.Data;
namespace MitechLib.Modules.PackagePage.ViewModels
{
public class CarrierInfoViewModel : ViewModelBase
{
private string _labelPitchX;
public string LabelPitchX
{
get { return _labelPitchX; }
set { _labelPitchX = value; }
}
public CarrierInfoViewModel()
{
LabelPitchX = "Pitch X (mm)";
}
}
}
```
3. Decorate the MEF `[Export] / [Import]` attributes
```cs
using MitechLib.Infrastructure.Data;
using System.ComponentModel.Composition;
namespace MitechLib.Modules.PackagePage.ViewModels
{
[Export]
public class CarrierInfoViewModel : ViewModelBase
{
private string _labelPitchX;
public string LabelPitchX
{
get { return _labelPitchX; }
set { _labelPitchX = value; }
}
public CarrierInfoViewModel()
{
LabelPitchX = "Pitch X-- (mm)";
}
}
}
```
```cs
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using System.Windows.Controls;
namespace MitechLib.Modules.PackagePage.Views
{
///
/// Interaction logic for CarrierInfoView.xaml
///
[ViewSortHint("02")]
[PartCreationPolicy(CreationPolicy.Shared)]
[Export]
public partial class CarrierInfoView : UserControl
{
public CarrierInfoView()
{
InitializeComponent();
}
}
}
```
4. Setup DataContext Binding
```cs
using Microsoft.Practices.Prism.Regions;
using MitechLib.Modules.PackagePage.ViewModels;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Controls;
namespace MitechLib.Modules.PackagePage.Views
{
///
/// Interaction logic for CarrierInfoView.xaml
///
[ViewSortHint("02")]
[PartCreationPolicy(CreationPolicy.Shared)]
[Export]
public partial class CarrierInfoView : UserControl
{
public CarrierInfoView()
{
InitializeComponent();
}
[Import]
[SuppressMessage("Microsoft.Design", "CA1044:PropertiesShouldNotBeWriteOnly",
Justification = "Needs to be a property to be composed by MEF")]
public CarrierInfoViewModel ViewModel
{
set
{
this.DataContext = value;
}
}
}
}
```
```xml
...
..
```
5. Integration with Mitech view registration by updating the `ModuleViewNames.cs` related dictionary with `MitechLib.InfrastructureModules`
```cs
public static readonly Dictionary DeviceInfoRegion = new Dictionary
{
{"CassetteInfoView", "MitechLib.Modules.PackagePage.Views.CassetteInfoView"},
{"VisionSettingView", "MitechLib.Modules.PackagePage.Views.VisionSettingView"},
//
{"CarrierInfoView", "MitechLib.Modules.PackagePage.Views.CarrierInfoView"}
};
```
## Guide to link View command to ViewModel
1. Create `ICommand` variable in view model. Naming convention `Command`
2. Create `DelegateCommand` to the ICommand within the constructor
3. Create the Delegate Command callback function
```cs
[Export]
public class CarrierInfoViewModel : ViewModelBase, IHeaderInfoProvider
{
// 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
```
## 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
{
// Step1
private readonly IRegionManager regionManager;
// Step2
[ImportingConstructor]
public CarrierInfoViewModel(IRegionManager _regionManager)
{
//
regionManager = _regionManager;
}
// Step3
private void OnExitCommand()
{
regionManager.RequestNavigate(RegionNames.PackageMainViewRegion, new Uri("PackageMgmtView", UriKind.Relative));
}
}
```