2

Wpf Tuturialから WPF を学ぼうとしています

WPF と VB.Net の変換に問題があります。私はVS 2012を使用しています

ここに私のXMLコードがあります

<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コードがあります

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

上記のWebサイトのチュートリアルからVB.NetにC#コードを変換しようとしています

エラーは

        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.

助けてください

前もって感謝します

アミット・サラフ

編集。エラー。& 修正後の警告

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

1 に答える 1

1

コードには 2 つの問題があります。

まず、名前空間名は大文字と小文字が区別されます。N小文字にする必要があります。名前空間宣言の構文はclr-namespace.

第二に、YesNoネストされたクラスでBinding_Sample_Converterあってはならないクラス。あなたcan't create an instance of nested class in XAMLMSDNから:

オブジェクト要素としてインスタンス化できるようにするには、クラスが次の要件を満たす必要があります。

  1. カスタム クラスはパブリックであり、既定の (パラメーターなしの) パブリック コンストラクターをサポートする必要があります。

  2. カスタム クラスは、ネストされたクラスであってはなりません。入れ子になったクラスと、その一般的な CLR 使用構文の "ドット" は、添付プロパティなどの他の WPF や XAML 機能と干渉します。

XAML :

xmlns:local="clr-namespace:WPFTutorials.Converter"

コンバーター:

YesNoクラスをクラスの外に移動Public Class Binding_Sample_Converterし、名前空間の直下で宣言します。

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
于 2013-12-29T10:10:35.557 に答える