【问题标题】:How to pass a parameter to Singleton in MVVM Xamarin Forms如何在 MVVM Xamarin Forms 中将参数传递给 Singleton
【发布时间】:2019-07-22 10:31:26
【问题描述】:

我想从另一个 ViewModel 向 ObservableCollection 添加一个元素...问题是当通过单例生成该 ViewModel 的实例时,我收到构造函数接收参数的错误

没有给出与所需形式相对应的参数 参数 'centroLegado' 的 'GenerarRetiroCentroViewModel.GenerarRetiroCentroViewModel(CentroGeneracion)'

如何实现单例以便可以从另一个 ViewModel 调用它?

我附上了具有可观察集合和构造函数的 ViewModel 的代码..

通用RetiroCentroViewModel.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

我附上了我“希望”如何从另一个 ViewModel (SelectResiduoViewModel.CS) 调用我的 ObservableCollection 的代码

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);

如何添加 Mode 对象来填充另一个 ViewModel 中的控件?单例可以做到这一点吗?我该怎么做?对我有什么帮助吗?

【问题讨论】:

  • 关于该错误,您只需正确填写参数即可。您期待CentroGeneracion ,但您没有传递任何参数。如果您不需要它,请创建第二个空构造函数。此外,您永远不会分配 instance 属性,这意味着如果您调用该函数,您将始终返回一个 new 单例实例。注意:您的实现不是线程安全的。如果您不需要线程安全的线程安全的,那么您就可以使用了,否则您应该考虑重写它。
  • 另一方面,如果您仍然可以这样做,请考虑为所有内容仅使用英文名称 - 它很快就会变得混乱,然后您的课程更大,30%是您的母语,其余的是英文.另外,一段时间后,我发现用英语命名事物比用我自己的语言命名要容易得多
  • 你不建议回答这个问题吗?不鼓励?或者子进程的实现非常不稳定? @肖恩
  • @BodegaPangal 我其实很想但是忘记了¯_(ツ)_/¯

标签: c# xamarin mvvm xamarin.forms viewmodel


【解决方案1】:

(我居然忘记回答了。对不起¯\_(ツ)_/¯)

正如我在评论中所解释的,您只需正确填写参数即可。您期待CentroGeneracion,但您没有传递任何参数。如果您不需要它,请创建第二个空构造函数。

此外,您永远不会分配实例属性,这意味着如果您调用该函数,您将始终返回一个新的单例实例。 应该是

public GenerarRetiroCentroViewModel GetInstance()
{
    if(instance == null){
        instance = new GenerarRetiroCentroViewModel();
    }

    return instance;
}

其实你甚至不需要函数。

public GenerarRetiroCentroViewModel Instance
{
    get
    {
        if(instance == null){
            instance = new GenerarRetiroCentroViewModel();
        }

        return instance;
    }
}

注意:这不是线程安全的。 如果你想让它成为线程安全的,你可以使用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;
        }
    }

}

它基本上会在有人第一次尝试访问该属性时创建一个 Singleton。

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2018-03-15
    • 1970-01-01
    • 2016-10-30
    • 2019-08-19
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多