wpf 该名称在命名空间“clr-namespace:”中不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22128311/
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-13 11:05:26  来源:igfitidea点击:

The name does not exist in the namespace "clr-namespace:"

wpfvb.netvisual-studio-2012mvvm

提问by Krowi

This is my MainWindowViewModel.vb

这是我的 MainWindowViewModel.vb

Public Class MainWindowViewModel
Inherits ViewModelBase

Public Sub New()

End Sub

Private _test As String = "Success"
Public Property Test() As String
    Get
        Return _test
    End Get
    Set(ByVal value As String)
        _test = value
        RaisePropertyChanged()
    End Set
End Property

End Class

This is my MainWindow.xaml

这是我的 MainWindow.xaml

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Koala"  
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:MainWindowViewModel x:Key="MainWindowViewModelDataSource" d:IsDataSource="True"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MainWindowViewModelDataSource}}">
    <TextBox HorizontalAlignment="Left" Height="23" Margin="156,103,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Test}"/>
</Grid>
</Window>

And this is the error I get:

这是我得到的错误:

Error   1   The name "MainWindowViewModel" does not exist in the namespace "clr-namespace:Koala".   \psf\Dropbox\Programming\Windows\Koala\Koala\MainWindow.xaml   7   3   Koala

I can add a reference to the namespace Koala in the Window tag without problems. When I type <local:, I get the option to use the MainWindowViewModeltag. Whenever I add the x:Keyor not, from that moment the error occurs. (Re)building doesn't work, restarting VS2012 doesn't work. I searched dozens of similar problems but it I can't find a fix for mine.

我可以毫无问题地在 Window 标记中添加对命名空间 Koala 的引用。当我输入时<local:,我可以选择使用MainWindowViewModel标签。每当我添加x:Key或不添加时,从那一刻起就会发生错误。(重新)构建不起作用,重新启动 VS2012 不起作用。我搜索了数十个类似的问题,但找不到适合我的解决方案。

I tried to add the reference through Blend and VS2012 but neither of them work. The odd thing is that I'm able to start the debug mode and see my form on the screen. Also, Successappears in the TextBox.

我尝试通过 Blend 和 VS2012 添加引用,但它们都不起作用。奇怪的是,我能够启动调试模式并在屏幕上看到我的表单。此外,Success出现在TextBox.

采纳答案by safetyOtter

Wrap your MainWindowViewModel class in

将您的 MainWindowViewModel 类包装在

Namespace ViewModels
  Public Class MainWindowViewModel
  Inherits ViewModelBase
...
  End Class
End Namespace

And then add a new namespace to your window:

然后在你的窗口中添加一个新的命名空间:

xmlns:vm="clr-namespace:Koala.ViewModels"

And use the vm prefix instead of local in your resources declaration.

并在资源声明中使用 vm 前缀而不是 local。

回答by resp78

I had a similar problem with two UserControls present in two different dlls. I resolved the issue by adding assembly reference to the xaml like below

我在两个不同的 dll 中存在两个 UserControl 时遇到了类似的问题。我通过向 xaml 添加程序集引用解决了这个问题,如下所示

<Window x:Class="View.MainWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v1="clr-namespace:View1;assembly=MyAssemblyName1"
    xmlns:v2="clr-namespace:View2;assembly=MyAssemblyName2"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <v1:UserControl1/>
        <v2:UserControl2/>
    </Grid>
</Window>

My project is a .Net4.5 project with two dlls containing two UserControls and a Main WPF application

我的项目是一个 .Net4.5 项目,其中有两个 dll,其中包含两个 UserControl 和一个 Main WPF 应用程序

Please note the addition

请注意添加

assembly=MyAssemblyName1