none
Why does the XAML designer run code from unrelated view models?

    Question

  • I recently had a problem whereby loading a XAML file would show something like the following...

    Despite my efforts to prevent this by wrapping the code in question with this...

    if (!IsInDesignMode) {
      // Run the code that was causing the exception
    }

    ...I still got the exception.

    After looking more closely, I realised that the view model mentioned in the stack trace was not the view model associated with the XAML file I was editing. When I added the code above to the other view model, the exception went away.

    What bothers me is why Visual Studio was running code in that view model at all. I hadn't opened that file, hadn't accessed the window that used it when I was debugging, nothing. It seemed to have picked that one at random and run the code.

    Is there any way to prevent this from happening?

    Tuesday, March 21, 2017 3:38 PM

All replies

  • Hi Mr Yossu,

    Would you please share a simple sample using one drive? So we could test it in our side using the same Environment as yours.

    >>I recently had a problem whereby loading a XAML file would show something like the following...

    Do you mean that you get this Exception during you open the Designer file or during debugging time?

    Would you please share us more information about where you get this exception messages?

    (1)If it showed the error during you open the Designer file, what is your VS version? Is it the latest version for your current VS? The reason is that I remember that I got the stack trace designer view issue before which was related to the VS update package(not the latest version).

    (2)If it has this issue during debugging time, how about the result if you run it directly with "start without debugging(Ctrl+F5)"?

    If possible, you could share us a simple sample using one drive, so we could troubleshoot this issue and make sure that whether it is related to the VS IDE or the project itself or others. Please also share us the VS Environment in your side so we could set up the same Environment as yours.

    If there's any concern, please feel free to let me know.

    Sincerely,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 22, 2017 8:20 AM
    Moderator
  • I'm not sure I could reproduce the issue. I only found out about it running the unrelated code when I saw the exception in the XAML designer, and as I don't really know why I was getting the exception, I'm not going to be able to reproduce it in a sample project.

    To clarify, the exception appears in Visual Studio in the XAML designer when I open a XAML file to modify it. I'm not running the code at this stage.

    I'm using Visual Studio Enterprise 2015 version 14.0.25431.01 Update 3 on Windows 10 Professional x-64.

    Does that help? Thanks

    Wednesday, March 22, 2017 2:12 PM
  • Hi Mr Yossu,

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 23, 2017 6:55 AM
  • Did you ever get any further one this one? It's still happening.
    Tuesday, May 16, 2017 3:41 PM
  • Month later, still happening, still no response from Microsoft
    Wednesday, June 14, 2017 5:09 PM
  • Three months later and no response. I guess I should stop holding my breath eh?
    Thursday, June 29, 2017 3:49 PM