Kyle V.
Kyle V.

Reputation: 4792

Microsoft Visual Studio XAML UI Designer FileNotFoundException when Editing View

I'm using WPF with .NET 4.5.2 on Visual Studio 2012 and getting a FileNotFoundException in the XAML view when editing my View:

enter image description here

The application compiles and runs fine otherwise. The exception manifests as a blue underline only.

I debugged XDesProc.exe and found this to be the source of the exception. The reason being that its working directory is C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE so its looking for the config in the wrong place.

It occured to me that this might be a problem of design in the application. The exception is being thrown from in MainWindowViewModel.cs on this line:

RemotingConfiguration.Configure("ZZZZZ.exe.config", false);

I believe this line is being reached when the XAML UI Designer reaches this section of my View.xaml:

<Window.DataContext>
    <vm:MainWindowViewModel/>
</Window.DataContext>

Which is where I'm instantiating my ViewModel and assigning it to the DataContext.

I think I need to move RemotingConfiguration.Configure() outside of my ViewModel constructor but I don't know where to put it. I believe this to be the solution to my problem.

Thanks in advance!

Upvotes: 1

Views: 197

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31656

In design mode controls and instances which are specified in the xaml are instantiated. Within your VM (or other areas) there may be code which should not be attempted during design mode.

Its best to segregate out those sections by specifying whether or not to attempt it if it is design mode such as

if (!DesignerProperties.GetIsInDesignMode(this)) { ... } // if not in design mode.

Since the VM is the issue, here are the options.

  • Instantiate the VM in code behind and when in design mode, don't instantiate it using the above logic check.
  • If the VM has to be instantiated on the xaml for religious reasons then follow these steps.
    1. Name the VM in Xaml by adding the Name attribute on the VM in the Xaml.
    2. Create a Boolean (does not have to adhere to INotifyPropertyChanged) on the VM named IsInDesign.
    3. In the main page constructor check if in design mode using above logic and assign that value to the VM's IsInDesign to the named VM.
    4. In the constructor of the VM (which should instantiate after the Main page's constructor) check the value of IsInDesign and stop any loading/reading of files depending on its value.

Upvotes: 1

Related Questions