名称“YesNo"命名空间中不存在“clr-Namespace:WPF_Tutorial.WPFTutorials.Converter"; [英] The name "YesNo" does not exist in the namespace "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter"
问题描述
我正在尝试从 Wpf Tuturial<学习 WPF/a>
I'm trying to Learn WPF from Wpf Tuturial
我在 WPF 和 VB.Net 中遇到转换问题.我正在使用 VS 2012
I'm having problem with converts in WPF and VB.Net. I'm Using VS 2012
这是我的 XML 代码
Here is my XML Code
<Window x:Class="Binding_Sample_Converter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-Namespace:WPF_Tutorial.WPFTutorials.Converter"
Title="Binding_Sample_Converter" Height="300" Width="300">
<Window.Resources>
<!--
Error 1 The name "YesNo" does not exist in the namespace "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter".
Error 2 The name "YesNo" does not exist in the namespace "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter".
Error 3 The type 'local:YesNo' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
-->
<local:YesNo x:Key="YesNo" />
</Window.Resources>
<Grid>
</Grid>
这是我的 VB.Net 代码
Here is my VB.Net Code
Imports System.Windows
Imports System.ComponentModel
Imports System.Windows.Data
Namespace WPFTutorials.Converter
Public Class Binding_Sample_Converter
Public Class YesNo
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Select Case value.ToString.ToLower
Case "yes"
Return True
Case "no"
Return False
End Select
Return False
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
If TypeOf value Is Boolean Then
If CBool(value) = True Then : Return "yes"
Else : Return "no"
End If
Else : Return "no"
End If
End Function
End Class
End Class
End Namespace
我正在尝试将上述网站教程中的 C# 代码转换为 VB.Net
I'm trying to convert C# code from said website tutorial to VB.Net
错误是
Error 1 The name "YesNo" does not exist in the namespace "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter".
Error 2 The name "YesNo" does not exist in the namespace "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter".
Error 3 The type 'local:YesNo' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
请帮忙
提前致谢
阿米特·萨拉夫
编辑.呃.&修改后警告
Edit. Err. & Warning after modification
<!--
Warning 1 Namespace or type specified in the Imports 'WPFTutorials.Converter' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. D:\Data - 2012\WPF\WPF_Tutotrial\obj\Debug\Binding_Sample_Converter.g.i.vb 35 9 WPF_Tutotrial
Error 2 The name "YesNo" does not exist in the namespace "clr-namespace:WPFTutorials.Converter". D:\Data - 2012\WPF\WPF_Tutotrial\Binding_Sample_Converter.xaml 11 9 WPF_Tutotrial
Error 3 The type 'local:YesNo' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\Data - 2012\WPF\WPF_Tutotrial\Binding_Sample_Converter.xaml 12 10 WPF_Tutotrial
-->
推荐答案
您的代码中有两个问题.
There are two issues in your code.
首先,命名空间名称区分大小写.N
应该是小写.命名空间声明的语法是 clr-namespace
.
First, Namespace name is case sensitive. N
should be lower case. Syntax for namespace declaration is clr-namespace
.
第二个,Binding_Sample_Converter
嵌套类中的YesNo
类,它不应该是.不能在 XAML 中创建嵌套类的实例
.来自 MSDN:
Second, YesNo
class in nested class of Binding_Sample_Converter
which it shouldn't be. You can't create an instance of nested class in XAML
. From MSDN:
为了能够实例化为对象元素,您的类必须满足以下要求:
In order to be able to be instantiated as an object element, your class must meet the following requirements:
您的自定义类必须公开并支持默认(无参数)公共构造函数.
Your custom class must be public and support a default (parameterless) public constructor.
您的自定义类不能是嵌套类.嵌套类和点"它们的通用 CLR 使用语法会干扰其他 WPF 和/或 XAML附加属性等功能.
Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.
XAML:
xmlns:local="clr-namespace:WPFTutorials.Converter"
转换器:
将YesNo
类移出Public Class Binding_Sample_Converter
类,并直接在命名空间下声明.
Move YesNo
class out of class Public Class Binding_Sample_Converter
and declare it directly under the namespace.
Namespace WPFTutorials.Converter
Public Class Binding_Sample_Converter
End Class
Public Class YesNo
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Select Case value.ToString.ToLower
Case "yes"
Return True
Case "no"
Return False
End Select
Return False
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
If TypeOf value Is Boolean Then
If CBool(value) = True Then : Return "yes"
Else : Return "no"
End If
Else : Return "no"
End If
End Function
End Class
End Namespace
这篇关于名称“YesNo"命名空间中不存在“clr-Namespace:WPF_Tutorial.WPFTutorials.Converter";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!