簡體   English   中英

將自定義轉換器作為資源添加到XAML頁面中,作為VB.Net中的資源

[英]Add the custom converter into XAML page as resource in VB.Net

我已經創建了一個自定義轉換器。 我嘗試在頁面中定義它。 我在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

項目布局:

在此輸入圖像描述

首先,我將您的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" /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM