question

ManeeshaRajaratne-4635 avatar image
0 Votes"
ManeeshaRajaratne-4635 asked yanxiaodi commented

Opening a New Window on a Button click - MVVM WPF

I am new to MVVM architecture, and I would like to keep the standard of MVVM without violating its rules. So I implemented an approach to open a new window on a button click using Services. I don't know if this is the right approach to do so. Here is my code,
Inside the service folder, I have two files. IwindowService and WindowNavService.

 interface IWindowService { 
     void CreateWindow();    
 }
    
 class WindowNavService : IWindowService {       
       public void CreateWindow(){  
          PrintPreview printPreview = new PrintPreview {              
          DataContext = new PrintPreviewViewModel()                    
          };            
          printPreview.Show(); 
          }    
        }

PrintPreview.xaml is the second window that I want to open.
this is my ViewModel of the main Window.

 class PatientRecordDetailsViewModel {       
   WindowNavService windowNav = new WindowNavService();  
               
            //constructor         
           // some other codes        
           // button command         
   public ICommand SubmitCommand        
   {            
      get => new PatientRecordDetailsCommand(param => this.Submit(), param => CanSubmit());        
   }        
   private void Submit()        
   {            
      IWindowService windowService = windowNav;            
      windowService.CreateWindow();                    
   }        
   private bool CanSubmit()       
   {            
     return true;        
   }
 }

I am calling the method of the interface from the button Submit() method. It works fine. But what I need to know is that, if it is violating the standards of MVVM?



windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered yanxiaodi commented

Hi, MVVM is not a dogma.

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls.

View is the collection of visible elements, which also receives user input. This includes user interfaces (UI), animations and text. The content of View is not interacted with directly to change what is presented.

ViewModel is located between the View and Model layers. This is where the controls for interacting with View are housed, while binding is used to connect the UI elements in View to the controls in ViewModel.

It is ok that the ViewModel creates a new view as a result of the business logic and activate (displays) it. This does not violate the separation of design and logic.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank You Peter!, Is it fine if we can keep a separate service to do that?

0 Votes 0 ·
yanxiaodi avatar image yanxiaodi ManeeshaRajaratne-4635 ·

It looks good to me. Just my two cents, you already used interface so why not using DI to inject the instance of IWindowService to your viewmodel? so that no need to use new everywhere.

0 Votes 0 ·
AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered

Welcome to our Microsoft Q&A platform!

The purpose of MVVM is to separate the UI and business functions. The responsibility of View is to be responsible for how to display data and send commands. The function of ViewModel is to provide data and execute commands. Each department does its job without affecting each other.

Thanks.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.