clr-namespace:MyProject.ViewModels
There has to be a namespace called MyProject.ViewModels
that has a class Called MainPageViewModel
that is public and has a public parameterless constructor within the same assembly as ProjectDatabaseRebuilder.MainWindow
.
There is not.
If MyProject.ViewModels
exists in a referenced assembly, you must state so within the xmlns.
xmlns:vm="clr-namespace:MyProject.ViewModels;assembly=MyProject"
or some such. Honestly, it looks like you copypasted someone's example without realizing how these specialized xml namespaces work in WPF.
Something tells me the ultimate answer will be the following: xmlns:vm="clr-namespace:ProjectDatabaseRebuilder.ViewModels"
.
Please note that "namespace" and (as above) "assembly" mean namespaces and assemblies, and the xaml deserializer uses this information to locate types at runtime. If they are incorrect, things won't work.
This is trivial. You must have done something weird with your project, which might necessitate you start from scratch. Or, you can make a new project following my guide below, and then compare it bit by bit to yours to see where you went wrong.
First, create a new WPF application called MyWpfApplication.
Add a folder for Views and one for ViewModels. Add the shown VM code class and view UserControl:
In your code class, add the following:
namespace MyWpfApplication.ViewModels
{
class MainWindowViewModel
{
public string Text { get; set; }
public MainWindowViewModel()
{
Text = "It works.";
}
}
}
Your View is also simple:
<UserControl
x:Class="MyWpfApplication.Views.MainWindowView"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid>
<TextBlock
Text="{Binding Text}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</UserControl>
And, in your Window, do essentially what you are attempting to do:
<Window
x:Class="MyWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:MyWpfApplication.Views"
xmlns:vm="clr-namespace:MyWpfApplication.ViewModels"
Title="DERP"
Content="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate
DataType="{x:Type vm:MainWindowViewModel}">
<v:MainWindowView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
</Window>
And, when run, you should see everything as expected:
Do this, then compare the working solution to your project. If you can't find any differences, you probably need to just scrap your solution and start fresh. Copy code, not files, into the new solution.
Visual Studio expects namespaces to match folder locations.
To solve your problem, exit Visual Studio and rename your project folder to MyProject
. Then start Visual Studio, remove the project from the solution, add it again as "existing project" and build the project, F6 or ctrl + shift + B
If you rename your namespace after the project has been created you will get these kinds of bugs.
I had the same problem. I had the right namespace, the right class name, and I even updated the PATH. Finally, I took out the "DataType" from the XAML, got it to compile, the added the DataType back in and it worked. Seems like it was a chicken and egg problem. The XAML doesn't see it until it has been compiled in, and you can't compile with it in the DataType. So, create the MV first, and compile it. Then, add to XAML.
Rebuild your solution (sometimes clean then build works better). Then look at your error list, scroll to the very bottom, and it will most likely indicate an error that is not allowing your assembly to compile, and the XAML compiler is most likely using a cached version of the assembly, not the new one you mean to build.
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