MVVMLight UserControl View Model-为每个视图创建新的用户控件实例 [英] MVVMLight UserControl View Model-Create new Instance of User control for each view

查看:13
本文介绍了MVVMLight UserControl View Model-为每个视图创建新的用户控件实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在其他视图中使用的患者列表进行了用户控制.但是,当我选择其中一个患者时,选择会传播到包含用户控件实例的所有视图.如何让每个视图为每个视图实例化一个新的用户控件实例?我正在使用 C#

I have a user control of list of Patients which I use in other Views. However when I choose one of the Patients, the selection is propagated to all the views containing an instance of the user control. How can I make each view instantiate a new instance of the user control for each view? I am using c#

推荐答案

根据您所说的猜测,我假设您从定位器返回 PatientViewModel 的静态实例.要解决这个问题,请确保在调用该属性时生成一个新的视图模型实例.

Guessing from what you stated, I'd assume that you returning a static instance of you PatientViewModel from you locator. To solve this make sure that when the property is called a new instance of the view model is generated.

具有不同实例化方法的定位器

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            //if (ViewModelBase.IsInDesignModeStatic)
            //{
            //    // Create design time view services and models
            //    SimpleIoc.Default.Register<IDataService, DesignDataService>();
            //}
            //else
            //{
            //    // Create run time view services and models
            //    SimpleIoc.Default.Register<IDataService, DataService>();
            //}

        SimpleIoc.Default.Register<MainViewModel>();
    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

      public MainViewModel StaticMain
      {
          get
          {
              return _staticMain ?? (_staticMain = new MainViewModel());
          }
      }
      private static MainViewModel _staticMain;


      public MainViewModel NewMain
      {
          get
          {
              return new MainViewModel();
          }
      }

      public MainViewModel NewIocMain
      {
          get
          {
              return ServiceLocator.Current.GetInstance<MainViewModel>(Guid.NewGuid().ToString());
          }
      } 

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
}

  1. Main 属性使用 Lauent 的 SimpleIoc 容器来创建实例.该容器的一个属性是,它将每种类型都视为单例.因此,如果您使用这种虚拟机生成方法,您将在整个视图中共享虚拟机.

  1. The Main property uses Lauent's SimpleIoc container to create an instance. One of the properties of this container is, that it treats every type as singleton. Thus if you use this VM generation method you will share the VM throughout the views.

StaticMain 属性的作用大致相同,但它不是使用 Laurent 的容器,而是保存一个也在视图之间共享的 VM 的静态实例.

The StaticMain property does much the same, but instead of using Laurent's container it holds a static instance of the VM which is also shared between the views.

NewMain 属性会在每次调用时创建一个新 VM,因此,VM 不会在视图之间共享.

The NewMain property creates a new VM upon every call, therefore, the VM is not shared between the views.

NewIocMain 属性还会在每次调用时创建一个新 VM,因此 VM 不会在视图之间共享.但是,SimpleIoc 容器包含对所创建实例的引用.它不会自动释放此实例,您必须使用您用于创建的密钥(Guid)调用 SimpleIoc.Default.Unregister(key) 以在不再需要时从容器中删除该实例它.

The NewIocMain property also creates a new VM upon every call and the VM is, therefore, not shared between the views. However, the SimpleIoc container holds a reference to the instance created. It does not release this instance automatically and you have to call SimpleIoc.Default.Unregister(key) with the key you used for creation (the Guid) to remove the instance from the container once you no longer need it.

除了使用 SimpleIoc 之外,您显然可以选择使用另一个 IOC 容器(例如 Unity),它可以让您更好地控制实例的创建方式以及它们的生存时间.除此之外,我会根据你的情况选择 NewMain 方法.

Instead of using the SimpleIoc you obviously can opt to use another IOC Container - such as Unity for example - that allows you greater control how your instances are created and how long they live. Barring this, I'd opt for the NewMain approach given yor case.

这篇关于MVVMLight UserControl View Model-为每个视图创建新的用户控件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆