名称ViewModel在名称空间"clr-namespace:Project.ViewModels"中不存在. [英] The name ViewModel does not exist in the namespace "clr-namespace:Project.ViewModels"

查看:274
本文介绍了名称ViewModel在名称空间"clr-namespace:Project.ViewModels"中不存在.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,这确实是一个奇怪的错误.我正在开发WPF应用程序并关注MVVM.在我的MainWindow中,我正在设置视图和视图模型,但出现了这个奇怪的错误.尽管它可以正常运行并且应用程序可以正常运行,但是为什么会出现此错误.

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.

我也遵循了类似的方法,但是没有找到合适的答案.我尝试重新启动Visual Studio并清理和重建,但仍然遇到此错误.

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.

这是代码.

<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".

这是我的ViewModel

Here is my ViewModel

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

那么什么是真正的错误.顺便说一下,我正在使用Visual Studio 2012.

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

更新: 我的视图模型和视图在同一个项目中.我没有引用任何其他项目.并且存在MyProject.ViewModels.MainPageViewModel.

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

推荐答案

clr-namespace:MyProject.ViewModels

clr-namespace:MyProject.ViewModels

必须有一个名为MyProject.ViewModels的命名空间,该命名空间具有一个名为MainPageViewModel的类,该类是公共的,并且在与ProjectDatabaseRebuilder.MainWindow相同的程序集中具有公共的无参数构造函数.

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.

没有.

如果引用的程序集中存在MyProject.ViewModels,则必须在xmlns中声明.

If MyProject.ViewModels exists in a referenced assembly, you must state so within the xmlns.

xmlns:vm="clr-namespace:MyProject.ViewModels;assembly=MyProject"

或类似的东西.坦白地说,您似乎复制了某人的示例,而没有意识到这些专用的xml名称空间在WPF中的工作方式.

or some such. Honestly, it looks like you copypasted someone's example without realizing how these specialized xml namespaces work in WPF.

某些事情告诉我最终的答案将是:xmlns:vm="clr-namespace:ProjectDatabaseRebuilder.ViewModels".

Something tells me the ultimate answer will be the following: xmlns:vm="clr-namespace:ProjectDatabaseRebuilder.ViewModels".

请注意,名称空间"和(如上所述)程序集"表示名称空间和程序集,并且xaml反序列化程序使用此信息在运行时定位类型.如果它们不正确,则将无法正常工作.

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.

首先,创建一个名为MyWpfApplication的新WPF应用程序.

First, create a new WPF application called MyWpfApplication.

为View添加一个文件夹,为ViewModel添加一个文件夹.添加显示的VM代码类并查看UserControl:

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.";
        }
    }
}

您的视图也很简单:

<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>

然后,在您的Window中,基本上执行您要尝试执行的操作:

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.

这篇关于名称ViewModel在名称空间"clr-namespace:Project.ViewModels"中不存在.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆