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.
this is my ViewModel of the main Window.
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?