Two views - one ViewModel

前端 未结 3 1483
孤独总比滥情好
孤独总比滥情好 2021-01-15 18:17

I must have misunderstood the concept of ViewModels and Views. But at this moment I can\'t rebuild the application from ground and this time doing it better. My

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 18:41

    You could create a new class that has your LoadedFiles property and then each unique view model can reference this class. You can share the one class with these shared properties between multiple view models. I am using MVVMLight's Locator with an Autofac container to inject this class into each of my view models (basically using Inversion of Control and Dependency Injection).

    You can read up on Inversion of Control and Dependency Injection here.

    Some sample code-

    public MyClass
    {
        public List LoadedFiles{get; set;}
    }
    
    public ViewModelOne
    {
        public MyClass MyClassInstance {get; set;}
        public ViewModelOne(MyClass myclass)
        {
            MyClassInstance = myclass
        }
    }
    
    public ViewModelTwo
    {
        public MyClass MyClassInstance {get; set;}
        public ViewModelTwo(MyClass myclass)
        {
            MyClassInstance = myclass
        }
    }
    

    You could also use MVVMLight's Locator to set each View's DataContext to the appropriate View.

    
    

提交回复
热议问题