I'm working on a WPF app using prism (MVVM), and I'd like to create a View for print, which will not be displayed on GUI.
My current code is shown below, but I'd like to avoid directly instantiating a View via its constructor.
void ExecutePrint()
{
var vm = new CustomViewModel();
var view = new CustomView { DataContext = vm }; // ← I'd like to avoid this!
vm.Print();
}
How can I link a View with a ViewModle in a case like this using prism?
NOTE:
- In the code above,
CustomViewModelis instantiated every time theExecutePrintmethod is called. Therefore,CustomViewModelcan not be registered as a singleton. - The instance of
CustomViewis used only for print. It should never be displayed on GUI. - In the code above,
vm.Print()perfectly works.CustomViewModelcan print the instance ofCustomViewviaBehavior. All I want to do is to completevm.Print()without directly instantiatingCustomView.