Creating a new instance of an object each time method is called

后端 未结 3 715
野趣味
野趣味 2021-01-06 05:41
Messenger.Default.Register(this, message =>
{
    var adventurerWindowVM = SimpleIoc.Default.GetInstance();
           


        
相关标签:
3条回答
  • 2021-01-06 05:57

    Having given the other answer, I guess I can say the IoC container used here is just SimpleIoC from MvvmLight and to get a new instance of the VM on every GetInstance(...) all you need to do is pass in a unique key every time when trying to resolve an instance of the VM.

    So you can switch

    var adventurerWindowVM = SimpleIoc.Default.GetInstance<AdventurerViewModel>();
    

    to

    var adventurerWindowVM = SimpleIoc.Default.GetInstance<AdventurerViewModel>(System.Guid.NewGuid().ToString());
    

    However as mentioned by the author of MVVMLight Here these VM's will get cached and we need to get rid of them when no longer needed. In your case probably when the Window is closed.

    Thus I'd have that entire lambda something like:

    Messenger.Default.Register<OpenWindowMessage>(this, message =>
    {
        var uniqueKey = System.Guid.NewGuid().ToString();
        var adventurerWindowVM = SimpleIoc.Default.GetInstance<AdventurerViewModel>(uniqueKey);
        adventurerWindowVM.Adv = message.Argument;
        var adventurerWindow = new AdventurerView() 
        {
            DataContext = adventurerWindowVM
        };
        adventurerWindow.Closed += (sender, args) => SimpleIoc.Default.Unregister(uniqueKey);
        adventurerWindow.Show();
    });
    

    Note:

    While this is somewhat longer 3 lines compared to just creating a new VM yourself with (new AdventurerViewModel()) I still favor this because if you use an IoC container to manage LifeTime of your VM's, then have it manage them completely. Don't really like mix-n-match when not needed. Rather keep the IoC Container doing what it's meant to do.

    If you need more control over VM injection and Life-time management look at more sophisticated Ioc controllers such as Unity. SimpleIoC was just meant to be a simple get your feet "wet" in IoC kind of container and it does a very good job in that regard.

    0 讨论(0)
  • 2021-01-06 06:07

    I think you are trying to use the same instance of your ViewModel with multiple views. So the views will obviously overwrite each others viewmodel contents.

    What if you do this;

            Messenger.Default.Register<OpenWindowMessage>(this, message =>
        {
            var adventurerWindowVM = new AdventurerViewModel();
            adventurerWindowVM.Adv = message.Argument;
            var adventurerWindow = new AdventurerView() 
            {
                DataContext = adventurerWindowVM
            };
            adventurerWindow.Show();
        });
    
    0 讨论(0)
  • 2021-01-06 06:17

    It's a method call, passing in an anonymous method using a lambda expression.

    It looks like you are getting your AdventurerViewModel from some sort of IoC container. How is the IoC container configured? In particular, what is the scope of the objects it gives you back? If you have the IoC configured to create objects in singleton scope, for example, then you will always get back a reference to the same object each time. You may need to configure the scope of the object in your IoC container so that it gives you back a fresh copy every time.

    How you do that will depend on your IoC container. Without knowing which IoC framework you are using or seeing its configuration, it's impossible to make any further comment.

    0 讨论(0)
提交回复
热议问题