0

I have a WPF application. I have written the application in MVVM design pattern & calling My main View model from App.xaml.cs.

    public partial class App : Application
    {
      private MainViewModel myVM;
      **other variables declaration...................
      protected override void OnStartup(StartupEventArgs e)
      {
       udocVM = new MainViewModel(window, msgBoxService, udocBl, docBl);
       mainWindow.DataContext = udocVM;

      }
    }

So I am getting Code Analysis Critical Error for this:--

MainViewModel.MainViewModel(IWindowFactory, IMessageBoxService, IBl, IDocCenterBL, AutoResetEvent)' contains a call chain that results in a call to a virtual method defined by the class.

So how to fix this issue? Do I need to move this code in Xaml code behind file (MainWindow.xaml.cs)?

sushmitgos
  • 153
  • 3
  • 13
  • Make a non-override method called `OnLoad` with the code to set the `mainWindow.DataContext`, then in `OnStartup` call OnLoad. This will fix the CA2214 – Jeremy Thompson Jun 16 '17 at 07:27
  • I added private Onload() method & add all stuff into this & call it from Onstartup, still it's showing the FXcop error. @jeremy – sushmitgos Jun 16 '17 at 12:42
  • Cool, now all you need to do is call OnLoad from the Constructor instead of the override OnStartup. – Jeremy Thompson Jun 17 '17 at 01:48

0 Answers0