I need to pass an id to my viewmodel constructor. This id is external, it is passed to the webpage that contains the silverlight app. I am using the MVVM Light framework. I have seen several articles about passing parameters to the viewmodel, but in all of them the parameter is something that is global or that can be instantiated with no parameters. In my case I need a GUID id in the constructor of my viewmodel and it's passed from another app.
EDIT:
This is the code of the locator:
public class ViewModelLocator
{
public static IUnityContainer Container
{
get;
private set;
}
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator(Guid id)
{
Container = new UnityContainer();
}
/// <summary>
/// Cleans up all the resources.
/// </summary>
public static void Cleanup()
{
Container.Resolve<CautelaVM>().Cleanup();
}
#region Cautela Inventário
public CautelaVM CautelaInventario
{
get
{
return Container.Resolve<CautelaVM>();
}
}
#endregion
}
The locator is passed to the DataContext in the view like this:
DataContext="{Binding Source={StaticResource Locator}
EDIT 2:
Now it works. This is the code I added in the view ctor for registering the GUID to be used in the VM ctor:
ViewModel.ViewModelLocator.Container.RegisterType<CautelaVM>(new InjectionConstructor(id));