wpf 名称 ViewModel 在命名空间“clr-namespace:Project.ViewModels”中不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17025601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:50:08  来源:igfitidea点击:

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

wpfmvvmvisual-studio-2012

提问by fhnaseer

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.

现在这是一个真正奇怪的错误。我正在开发 WPF 应用程序并关注 MVVM。在我的 MainWindow 中,我正在设置视图和视图模型,但出现了这个奇怪的错误。虽然它构建良好并且应用程序运行良好,但为什么我收到此错误。

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.

我也遵循了一些类似的但没有找到合适的答案。我试图重新启动visual studio并清理和重建,但我仍然面临这个错误。

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

这是我的 ViewModel

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

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

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

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

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

回答by

clr-namespace:MyProject.ViewModels

clr 命名空间:MyProject.ViewModels

There has to be a namespace called MyProject.ViewModelsthat has a class Called MainPageViewModelthat is public and has a public parameterless constructor within the same assembly as ProjectDatabaseRebuilder.MainWindow.

必须MyProject.ViewModels有一个名为 Called的命名空间,该命名空间具有一个MainPageViewModel公共类 Called ,并且在与ProjectDatabaseRebuilder.MainWindow.

There is not.

那没有。

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

如果MyProject.ViewModels存在于引用的程序集中,则必须在 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.

或一些这样的。老实说,您似乎复制粘贴了某人的示例而没有意识到这些专门的 xml 命名空间在 WPF 中是如何工作的。

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

有人告诉我最终的答案将如下: 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.

请注意,“命名空间”和(如上)“程序集”表示命名空间和程序集,并且 xaml 反序列化器使用此信息在运行时定位类型。如果他们是不正确的,事情将无法工作。



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.

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

Add a folder for Views and one for ViewModels. Add the shown VM code class and view UserControl:

为 Views 添加一个文件夹,为 ViewModels 添加一个文件夹。添加显示的 VM 代码类并查看 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 中,基本上执行您尝试执行的操作:

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

这样做,然后将工作解决方案与您的项目进行比较。如果您找不到任何差异,您可能需要放弃您的解决方案并重新开始。将代码而非文件复制到新解决方案中。

回答by brandon

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, F6or ctrl+ shift+ B

Visual Studio 期望命名空间与文件夹位置匹配。
要解决您的问题,请退出 Visual Studio 并将您的项目文件夹重命名为MyProject. 然后启动Visual Studio,从溶液中取出的项目,再次添加为“现有项目”和建设项目,F6ctrl+ shift+B

If you rename your namespace after the project has been created you will get these kinds of bugs.

如果在创建项目后重命名命名空间,则会出现此类错误。

回答by Dan Hewett

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.

我有同样的问题。我有正确的命名空间,正确的类名,我什至更新了 PATH。最后,我从 XAML 中取出“DataType”,编译它,重新添加 DataType 并且它起作用了。好像是先有鸡还是先有蛋的问题。XAML 在编译之前不会看到它,并且您无法在 DataType 中使用它进行编译。所以,首先创建MV,然后编译它。然后,添加到 XAML。

回答by John C

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.

重建您的解决方案(有时清理然后构建效果更好)。然后查看您的错误列表,滚动到最底部,它很可能表示不允许您的程序集编译的错误,并且 XAML 编译器很可能使用的是程序集的缓存版本,而不是您使用的新版本建造的意思。

回答by Gordon Slysz

Close and reopen Visual Studio.

关闭并重新打开 Visual Studio。

I tried some of the answers here (I'm using VS2012 with Resharper), and I was doing everything ok, but still had the error. I was even able to navigate to my bound fields using Resharper's 'cntl'+click, but was still getting XAML compile errors and designer wouldn't show the design view. Closing visual studio and reopening fixed it for me.

我在这里尝试了一些答案(我将 VS2012 与 Resharper 一起使用),我做的一切都很好,但仍然有错误。我什至能够使用 Resharper 的“cntl”+click 导航到我的绑定字段,但仍然出现 XAML 编译错误并且设计器不会显示设计视图。关闭视觉工作室并重新打开为我修复了它。

回答by user2871239

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:

我有同样的问题。我使用 Intellisense 构建命名空间映射,但它没有包含程序集属性,所以它看起来像这样:

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.

清理它并建造它,它起作用了。

回答by kokabi

This error occurs because a xaml file references to unbuilt namespace.

出现此错误是因为 xaml 文件引用了未构建的命名空间。

My Solution [ in 3 steps ]:

我的解决方案 [分 3 个步骤]:

1- Comment a problematic file[s] and replace with empty version. for example for yours:

1- 评论有问题的文件 [s] 并替换为空版本。例如对于你的:

<!-- <Window ...> </Window> -->  <!--you orginal xaml code!, uncomment later-->

<Window                          <!-- temp xaml code, remove later -->
    x:Class="MyProject.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>

2- Clean and Rebuild the Project/Solution.

2- 清理并重建项目/解决方案。

3-Uncomment original xaml code and remove temp xaml code.

3-取消注释原始 xaml 代码并删除临时 xaml 代码。

it's done.

完成。

回答by lanwin

For us it was simply that the Dll Project was set to Platform Target x64in Visual Studio 2013. After setting it to Any. Everything starts working again.

对我们来说,只是在 Visual Studio 2013中将 Dll 项目设置为 Platform Target x64。将其设置为Any 之后。一切都重新开始工作。

回答by Greg Osborne

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.

检查 Visual Studio 中的配置管理器。确保所有项目匹配平台。我希望平台为 x64,但我的主要应用程序设置为 AnyCPU。将其设置为 x64 即可解决问题。

回答by JAB

I have been through this with Microsoft. The answer, in summary, is to add your build output folder to your PATH.

我和微软一起经历过这个。总之,答案是将您的构建输出文件夹添加到您的 PATH。

Apparently, the designer in Visual Studio loads DLLs and their dependencies. When it can't find a dependency, it fails silently and you get these types of errors.

显然,Visual Studio 中的设计器加载了 DLL 及其依赖项。当它找不到依赖项时,它会默默地失败,并且您会收到这些类型的错误。

Adding the build output folder to my PATH is not really an option in my case, because I build several different configurations and they all go to separate folders. The last thing I need to encounter some weird problem caused by a DLL being picked up from a location I hadn't anticipated.

在我的情况下,将构建输出文件夹添加到我的 PATH 并不是一个真正的选择,因为我构建了几个不同的配置,并且它们都转到单独的文件夹。我需要遇到的最后一件事是从我没有预料到的位置拾取 DLL 引起的一些奇怪的问题。

Edit: Sorry. In my haste, I should have pointed out that the folder(s) to add to the PATH are the folder(s) containing the dependent DLLs. In my case, this is the build output folder, but that may not be true in all cases.

编辑:对不起。匆忙中,我应该指出要添加到 PATH 的文件夹是包含依赖 DLL 的文件夹。就我而言,这是构建输出文件夹,但并非在所有情况下都是如此。