Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

wpf - The name ViewModel does not exist in the namespace "clr-namespace:Project.ViewModels"

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. Although it builds fine and application runs fine, but why I am getting this error.

I also followed some similar but didn't find appropriate answer. I tried to restart visual studio and clean and rebuild, but still I face this error.

So here is the code.

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:MyProject.Views"
        xmlns:vm="clr-namespace:MyProject.ViewModels"
        xmlns:p="clr-namespace:MyProject.Properties"
        Title="{x:Static p:Resources.Title}" Height="400" Width="750" MinHeight="400" MinWidth="750">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:MainPageViewModel}">
            <v:MainPageView/>
        </DataTemplate>
    </Window.Resources>

Error   1   The name "MainPageViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels".

Here is my ViewModel

namespace MyProject.ViewModels
{
    public class MainPageViewModel : PropertyChangedBase
    {
        public MainPageViewModel()
        {
        }
    }
}

So what is real error. I am using Visual Studio 2012 by the way.

Update: My viewmodels and views are in the same project. I am not referencing to any other project. And MyProject.ViewModels.MainPageViewModel exists.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...