How to create a View for print using prism (MVVM) without directly instantiating it via constructor

112 Views Asked by At

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:

  1. In the code above, CustomViewModel is instantiated every time the ExecutePrint method is called. Therefore, CustomViewModel can not be registered as a singleton.
  2. The instance of CustomView is used only for print. It should never be displayed on GUI.
  3. In the code above, vm.Print() perfectly works. CustomViewModel can print the instance of CustomView via Behavior. All I want to do is to complete vm.Print() without directly instantiating CustomView.
0

There are 0 best solutions below