The name ViewModel does not exist in the namespace “clr-namespace:Project.ViewModels”

前端 未结 18 1736
旧时难觅i
旧时难觅i 2020-12-04 23:55

Now that\'s a real strange error. I am working on a WPF application and following MVVM. In my MainWindow I am setting views and view models and I get this strange error. Alt

相关标签:
18条回答
  • 2020-12-05 00:45

    This one's been tying me in knots for hours.

    I have a VS2015 solution which is several years old. On my laptop, it builds beautifully, all the code is checked into TFS, but when I tried to build it on a different machine, I'd constantly get this message about the namespace not existing.

    Basically, this RepBaseWindow is a class inherited from the WPF Window class, with a few extra bells'n'whistles.

    public class RepBaseWindow : Window
    

    I'd tried a load of the suggestions on here, but no luck.

    The irony is that, despite this message, if I right-clicked on this faulty link and selected "View Code", it'd happily take me to this class. The namespace was fine, the class was fine... but WPF wasn't happy about it.

    If I tried to use Intellisense to find the namespace, I'd end up with the same markup... and the same Build Errors.

    I compared the working copy of the code with the TFS version, and realised the problem.

    I needed to go back to using an old copy of the WindowBase.dll file from 2012.

    Ridiculous, hey ?

    The working version of WindowBase.dll is 635kb in size, and shows this in the details tab:

    I'm just posting this answer to show that, sometimes, these errors have absolutely nothing to do with faulty WPF markup or wrongly-spelt namespace names.

    Hope this helps.

    0 讨论(0)
  • 2020-12-05 00:45

    I had the same problem and when I added a DEFAULT CONSTRUCTOR to my ViewModel and built my software (ctrl+Shift+B), it started realizing the ViewModel in the specified namespace!

    0 讨论(0)
  • 2020-12-05 00:47

    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:

    enter image description here

    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:

    enter image description here

    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.

    0 讨论(0)
  • 2020-12-05 00:50

    Check the Configuration Manager in Visual Studio. Make sure all projects match platform. I wanted platform to be x64, but my primary application was set to AnyCPU. Set that to x64 and it took care of the problem.

    0 讨论(0)
  • 2020-12-05 00:52

    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.

    0 讨论(0)
  • 2020-12-05 00:53

    I had the same problem. I'd used Intellisense to build the namespace mapping and it had not included the assembly attribute, so it looked like this:

    xmlns:converters="clr-namespace:XYZ.UI.Converters;
    

    when I compared it to a working Behavior in another window, I spotted the difference. When I changed it to

    xmlns:converters="clr-namespace:XYZ.UI.Converters;assembly=XYZ.UI"
    

    cleaned it and built it, it worked.

    0 讨论(0)
提交回复
热议问题