wpf 将自定义转换器作为 VB.Net 中的资源添加到 XAML 页面中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19385151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add the custom converter into XAML page as resource in VB.Net
提问by Korki Korkig
I've created a custom converter. I try to define it in a page. I can't find any good example for this in Vb.net. Because of namespaces difference between C#, the resources on the internet don't help me very much. For example on the Microsoft's page I don't see how it is defined on the XAML's header.
我创建了一个自定义转换器。我尝试在页面中定义它。我在 Vb.net 中找不到任何好的例子。由于 C# 之间的命名空间差异,互联网上的资源对我帮助不大。例如,在 Microsoft 的页面上,我没有看到它是如何在 XAML 的标头上定义的。
Here is what I did until now but get an error that the namespace is not founded.
这是我到现在为止所做的,但收到了未建立命名空间的错误。
Converter:
转换器:
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:
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
Project layout:
项目布局:


回答by Sheridan
First, I would move your Converterclasses to a folder named Converterswhich would be in your main project folder, not the AppCodefolder. Then you should be including a namespace in allof your classes like this (please forgive any VB.NET errors as I am a C# developer):
首先,我会将您的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
You should then be able to declare an XML namespace in your XAML pages like this:
然后,您应该能够在 XAML 页面中声明一个 XML 命名空间,如下所示:
xmlns:Converters="clr-namespace:LipasoftKlantFische.Converters"
If that still doesn't work, you can try this one, but the first one should work:
如果这仍然不起作用,您可以尝试这个,但第一个应该有效:
xmlns:Converters="clr-namespace:LipasoftKlantFische.Converters;assembly=LipasoftKlantFische"
Then you should be able to access your Converterlike this:
然后你应该能够Converter像这样访问你的:
<Converters:MyConverter x:Key="SearchTermConverter" />

