I want to add an element to an ObservableCollection from another ViewModel ... the problem is that when generating the instance of that ViewModel through a singleton I get the error that the constructor receives a parameter
There is no argument given that corresponds to the required formal parameter 'centroLegado' of 'GenerarRetiroCentroViewModel.GenerarRetiroCentroViewModel(CentroGeneracion)'
How can I implement the singleton so that I can call it from another ViewModel?
I attach the code of the ViewModel that has the Observable Collection and the constructor..
GenerarRetiroCentroViewModel.CS:
#region Constructor
public GenerarRetiroCentroViewModel(CentroGeneracion centroLegado)
{
instance = this;
ListaResiduosTemporales = new ObservableCollection<ResiduosTemporal>();
centroSeleccionado = centroLegado;
}
#endregion
#region Singleton
static GenerarRetiroCentroViewModel instance;
public static GenerarRetiroCentroViewModel GetInstance()
{
if (instance == null)
{
return new GenerarRetiroCentroViewModel();
}
return instance;
}
#endregion
I attach the code of how I "wish" to call my ObservableCollection from the other ViewModel (SelectResiduoViewModel.CS)
SeleccionarResiduoViewModel.CS:
var objeto = new ResiduosTemporal
{
IdResiduo = IdResiduo,
NombreResiduo = NombreResiduo,
IdEstimado = IdUnidad,
NombreEstimado = NombreUnidad,
IdContenedor = IdContenedor,
NombreContenedor = NombreContenedor,
};
var generarRetiroCentroViewModel = GenerarRetiroCentroViewModel.GetInstance();
generarRetiroViewModel.ListaResiduosTemporales.Add(objecto);
How can I add the Mode object to fill a control that is in another ViewModel? Is this possible with a SINGLETON? how can I do it? any help for me?
(I actually forgot to make an answer. Sorry ¯\\_(ツ)_/¯)
As explained in my comment, you simply have to fill the parameter correct. You're expecting a CentroGeneracion
but you're not passing any arguments. If you don't need it, create a second, empty constructor.
Also, you're never assigning the instance property, that means you'll always return a new singleton instance if you call that function. It should be
public GenerarRetiroCentroViewModel GetInstance()
{
if(instance == null){
instance = new GenerarRetiroCentroViewModel();
}
return instance;
}
Actually, you don't even need a function.
public GenerarRetiroCentroViewModel Instance
{
get
{
if(instance == null){
instance = new GenerarRetiroCentroViewModel();
}
return instance;
}
}
Note: That is not thread-safe. If you want to make it thread-safe, you can use Lazy (.NET 4+).
public sealed class SingletonClass
{
private static readonly Lazy<SingletonClass> _lazy = new Lazy<SingletonClass>(() => new SingletonClass());
public static SingletonClass Instance
{
get
{
return _lazy.Value;
}
}
}
It basically creates a Singleton the first time someone tries to access that property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.