我创建了一个自定义转换器。我尝试在页面中定义它。我在Vb.net中找不到任何好的例子。由于C#之间的命名空间差异,互联网上的资源对我没有多大帮助。例如,在微软的页面上,我看不到它是如何在XAML标题上定义的。
这是我到目前为止所做的事情,但是得到了命名空间未建立的错误。
转换器:
Public Class MyConverter
Implements IMultiValueConverter
Public Function Convert(values As Object(), targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Return values
End Function
Public Function ConvertBack(value As Object, targetTypes As Type(), parameter As Object,culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Throw New NotSupportedException()
End Function
End Class
XAML页面:
<Page x:Class="KlantFische"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="MyConverter"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1108"
Title="KlantFische">
<Grid>
<Grid.Resources>
<!-- ERROR: The name "MyConverter" does not exist in the namespace "MyConverter". -->
<local:MyConverter x:Key="SearchTermConverter" />
</Grid.Resources>
</Grid>
</Page>
Imports System.Data
Imports System.Collections.ObjectModel
Imports System.Windows.Threading
Imports System.Windows.Controls.Primitives
Imports System.Text
Imports System.Text.RegularExpressions
Partial Public Class KlantFische
Inherits Page
Public Sub New()
Me.InitializeComponent()
End Sub
End Class
项目布局:
答案 0 :(得分:3)
首先,我会将您的Converter
类移动到名为Converters
的文件夹中,该文件夹位于主项目文件夹中,而不是AppCode
文件夹中。那么你应该在你的类的所有中包含一个命名空间(请原谅任何VB.NET错误,因为我是C#开发人员):
Namespace LipasoftKlantFische.Converters
Public Class MyConverter
Implements IMultiValueConverter
Public Function Convert(values As Object(), targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Return values
End Function
Public Function ConvertBack(value As Object, targetTypes As Type(), parameter As Object,culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Throw New NotSupportedException()
End Function
End Class
End Namespace
然后,您应该能够在XAML页面中声明XML命名空间,如下所示:
xmlns:Converters="clr-namespace:LipasoftKlantFische.Converters"
如果仍然不起作用,你可以尝试这个,但第一个应该可以工作:
xmlns:Converters="clr-namespace:LipasoftKlantFische.Converters;assembly=LipasoftKlantFische"
然后您应该可以像这样访问Converter
:
<Converters:MyConverter x:Key="SearchTermConverter" />