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.
I have reproduced this error using a three project solution, with the specified dependencies between them:
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With