我在vb中有以下内容:
Namespace WpfSample
Class MainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Public Class BooleanToVisibilityConverter
Implements IValueConverter
Public Sub New()
End Sub
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value.Equals(True) Then
Return System.Windows.Visibility.Visible
Else
Return System.Windows.Visibility.Collapsed
End If
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
If value.Equals(System.Windows.Visibility.Visible) Then
Return True
Else
Return False
End If
End Function
End Class
End Namespace
然后我的XAML看起来像:
<Window x:Class="WpfSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfSample"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<l:BooleanToVisibilityConverter x:Key="converter" />
</Window.Resources>
<Grid>
<TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="112,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label x:Name="lblLabel" Content="{Binding Text, ElementName=txtName,UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding IsChecked,ElementName=chkShowLabel, Converter={StaticResource boolToVis}}"
HorizontalAlignment="Left" Margin="256,37,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkShowLabel" Content="Show Label" HorizontalAlignment="Left" Margin="112,65,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
我看到以下3个错误 名称“BooleanToVisibilityConverter”在名称空间“clr-namespace:WpfSample”中不存在。
找不到类型'l:BooleanToVisibilityConverter'。验证您是否缺少程序集引用,并且已构建所有引用的程序集。
标签'BooleanToVisibilityConverter'在XML命名空间'clr-namespace:WpfSample'中不存在。
答案 0 :(得分:0)
从示例中删除转换器的使用情况,然后首先构建项目。只有在成功构建之后,您才能在xaml中使用它。
您在Window.resources下的密钥名称是&#34; converter&#34;当你使用&#34; boolToVis&#34;在标签标签
您可以做的另一件事是在通过删除xaml中的转换器用法来构建项目之后,编写xmlns:l =。您的完整程序集名称将显示在列表中。这样就可以避免编写错误的汇编路径。