Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View doesn't find ViewModel in different Assembly

I am starting a new project and oriented my projectstructure on the structure recommended in this question.

Now I am seeing strange behaviour. When I am setting the datacontext in the View-XAML, it isn't found at runtime (getting a XamlParseException). When I set it in the constructor in the codebehind-file, everything is working just fine.

Is this official (documented) behaviour when using different assemblies, or am I doing something wrong?

The code:

Not Working:

MainView.xaml:

<UserControl x:Class="ViewsRoot.Views.MainView"             
         xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"            
         xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<UserControl.DataContext>
    <viewModels:ShellViewModel />
</UserControl.DataContext>

MainView.xaml.cs

public MainView() 
{
    InitializeComponent();
    // No DataContext set in codebehind-file    
}

Working:

MainView.xaml:

<UserControl x:Class="ViewsRoot.Views.MainView"             
         xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"            
         xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<!--<UserControl.DataContext>
    <viewModels:ShellViewModel />
</UserControl.DataContext> -->

MainView.xaml.cs:

public MainView()
{
    InitializeComponent();
    DataContext = new ViewModelsRoot.ShellViewModel();
}

Update:

The Exception-Text is:

{"The file or assembly \" ViewModelsRoot, PublicKeyToken = null \ "or one of its dependencies was not found. The system can not find the file specified."}

And the only inner Exception I can see is a System.IO.FileNotFoundException.

Update 2:

Thanks for the comments, but I haven't forgotten a namespace. I shortened it here for showing the code, but I double- and triplechecked (again). The DataContexts namespace is also filled in by intellisense. The whole <viewModels:ShellViewModel /> is written by intelli-sense. So it is found at designtime... ...so any more ideas?

Update 3: The xaml is "correctly" parsed as I am able to bind the DataContext to a class in the same assembly.

like image 569
basti Avatar asked Sep 19 '12 08:09

basti


1 Answers

I have reproduced this error using a three project solution, with the specified dependencies between them:

  • StartupProject → ViewsRoot
  • ViewsRoot → ViewModelsRoot
  • ViewModelsRoot

"StartupProject" has "exe" output type, while the other two have "dll".

In my case, I solved the problem by adding "ViewModelsRoot" to the References list of "StartupProject". It is not a coding problem, but rather a runtime problem, because "ViewModelsRoot.dll" is not copied to "StartupProject" output folder.

When you specify the DataContext in code-behind, Visual Studio notices the need for that "dll" and adds it to the output after compilation. This doesn't happen when setting the DataContext from XAML. It is tricky because "ViewModelsRoot" code is used from XAML with Reflection. Adding it to References list forces Visual Studio to copy the "dll" in both cases.

You can also copy "ViewModelsRoot.dll" to the output folder directly, but it will not be updated when you change the code.

like image 86
MatrixRonny Avatar answered Oct 05 '22 23:10

MatrixRonny