d:DesignInstance with an interface type

前端 未结 4 599
-上瘾入骨i
-上瘾入骨i 2020-12-06 09:39

I\'m binding an UI to an interface (which is implemented by several presenters, not accessible from the UI assembly).

I really like d:DesignInstance in designer beca

4条回答
  •  难免孤独
    2020-12-06 09:51

    I have just investigated more or less the same question... Actually, what I did is to follow the principles of MvvMLight. Precisely, I used a ViewModelLocator (which will be more or less static) so that the "right" ViewModel is injected at runtime or at design time. The magic lies in the function ViewModelBase.IsInDesignModeStatic provided by the MvvMLight framework. Finally, my ViewModelLocator class looks like

    public class ViewModelLocator
        {
            private static readonly IKernel _kernel;
    
            static ViewModelLocator()
            {
                _kernel = new StandardKernel();
                if (ViewModelBase.IsInDesignModeStatic)
                {
                     _kernel.Bind().To();
                }
                else
                {
                    _kernel.Bind().To();
                }
            }
    
            public IBasicVM BasicVm { get { return _kernel.Get(); } }
        }
    

    You may ignore the Ninject _kernel but you may need it (or a similar Ioc) if you are building your ViewModels with an IoC.

    The App.xaml declares the ViewModelLocator as a ressource

      
        
          
        
      
    

    The MainWindow.DataContext property is bound to the BasicVM member of the ViewModelLocator. The Text property is bound the the GetContent member of the interface IBasicVM which is recognized statically by R# (at least R# 7.1 with VS2012)

    
        
            
        
    
    

    You may check this repository which I have created as a template.

提交回复
热议问题