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,
CustomViewModel
is instantiated every time theExecutePrint
method is called. Therefore,CustomViewModel
can not be registered as a singleton. - The instance of
CustomView
is used only for print. It should never be displayed on GUI. - In the code above,
vm.Print()
perfectly works.CustomViewModel
can print the instance ofCustomView
viaBehavior
. All I want to do is to completevm.Print()
without directly instantiatingCustomView
.