The name “YesNo” does not exist in the namespace “clr-Namespace:WPF_Tutorial.WPFTutorials.Converter”

依然范特西╮ 提交于 2019-12-12 18:23:32

问题


I'm trying to Learn WPF from Wpf Tuturial

I'm having problem with converts in WPF and VB.Net. I'm Using VS 2012

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>

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

I'm trying to convert C# code from said website tutorial to VB.Net

The Err is

        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.

Please Help

Thanks in advance

Amit Saraf

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

回答1:


There are two issues in your code.

First, Namespace name is case sensitive. N should be lower case. Syntax for namespace declaration is clr-namespace.

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:

  1. Your custom class must be public and support a default (parameterless) public constructor.

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

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


来源:https://stackoverflow.com/questions/20824032/the-name-yesno-does-not-exist-in-the-namespace-clr-namespacewpf-tutorial-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!