本文介绍了WPF命名空间斗争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 非常感谢帮助让这段代码正常工作。 我正在使用VisualStudio 2012,使用WPF和VB.NET。 我有一个名为MyNameSpace的命名空间。它在解决方案级别定义 它内部有很多项目。 我是试图让ValueConverter被识别,以便我可以使用它。 目的是将DateTime类型的值(包括日期和时间)转换为简单的MM / dd / yyyy 下面是一个项目中窗口的代码,后面跟着窗口的代码隐藏。 非常感谢。 MyWin.XAML: < 窗口 x:Class = MyWin xmlns = http: //schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml 标题 = MyWin 高度 = 300 宽度 = 300 xmlns:local = clr-namespace:MyNameSpace > < Window.Resources > <! - 下一行错误:名称空间中名称DateConverter - > < local:DateConvertor x:Key = MyDateConverter / > < / Window.Resources > < 网格 > < DataGrid > < DataGrid.Columns > < DataGridTemplateColumn 宽度 = 自动 标题 = Do By > < DataGridTemplateColumn.CellTemplate > < DataTemplate > < TextBlock 保证金 = 4 文字 = {Binding Path = DoByDate} / > <! - 无法使用转换器显示MM / dd / yyyy。 - > <! - < TextBlock Margin =4Text ={Binding Path = DoByDate,Converter = {StaticResource DateConvertor}}/> - > < / DataTemplate > < / DataGridTemplateColumn.CellTemplate > < DataGridTemplateColumn.CellEditingTemplate > < ; DataTemplate > < DatePicker SelectedDate = {Binding Path = DoByDate,Mode = TwoWay} / > < / DataTemplate > < / DataGridTemplateColumn.CellEditingTemplate > < / DataGridTemplateColumn > < /DataGrid.Columns > < / DataGrid > < / Grid > < / Window > MyWin.XAML.VB: 进口 System.Globalization 公共 类 MyWin 公共 类 DateConvertor 实现 IValueConverter 公共 功能转换(值作为 对象,targetType 作为类型,参数作为 对象,culture As System.Globalization.CultureInfo)作为 对象 实现 IValueConverter.Convert 返回 DirectCast (value,DateTime).ToString( MM / dd / yyyy) End 功能 公共 功能 ConvertBack(value As Object ,targetType As 类型,参数作为 对象,culture As System.Globalization.CultureInfo)作为 对象 Implements IValueConverter.ConvertBack 返回 DateTime.ParseExact(value.ToString(), ddMMyyyy,CultureInfo.InvariantCulture) 结束 功能 结束 类 公开 Sub 新() ' 此调用是设计者所必需的。 InitializeComponent() ' 在InitializeComponent()调用后添加任何初始化。 结束 Sub 结束 Class 解决方案 如果它在另一个程序集中,那么你必须添加程序集信息。即: xmlns:Common =clr-namespace:Fox.UI.Resource; assembly = Fox.UI.Resource xmlns:local =clr-namespace:LSE什么是 LSE ,如果你写的那个你使用命名空间 MyNameSpace ?使用 MyNameSpace 而不是 LSE Would appreciate help to get this code to work.I'm using VisualStudio 2012, using WPF with VB.NET.I have a namespace named MyNameSpace. It's defined at the solution levelIt has a number of projects within it.I'm trying to get a ValueConverter to be recognized so that I can use it.Purpose is to convert a value of type DateTime, which includes date and time, to simply MM/dd/yyyyBelow is code for a window in one project, followed by the window's code-behind.Thank you very much.MyWin.XAML:<Window x:Class="MyWin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MyWin" Height="300" Width="300" xmlns:local="clr-namespace:MyNameSpace"> <Window.Resources> <!-- ERROR ON NEXT LINE: The name DateConverter does not exist in the namespace--> <local:DateConvertor x:Key="MyDateConverter"/> </Window.Resources> <Grid> <DataGrid> <DataGrid.Columns> <DataGridTemplateColumn Width="auto" Header="Do By"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Margin="4" Text="{Binding Path=DoByDate}" /> <!-- Cannot use the converter to show MM/dd/yyyy.--><!-- <TextBlock Margin="4" Text="{Binding Path=DoByDate, Converter={StaticResource DateConvertor}}" /> --> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <DatePicker SelectedDate="{Binding Path=DoByDate, Mode=TwoWay}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid></Window>MyWin.XAML.VB:Imports System.GlobalizationPublic Class MyWin Public Class DateConvertor Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert Return DirectCast(value, DateTime).ToString("MM/dd/yyyy") End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack Return DateTime.ParseExact(value.ToString(), "ddMMyyyy", CultureInfo.InvariantCulture) End Function End Class Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End SubEnd Class 解决方案 If it is in another assembly, then you have to add the assembly informaton. i.e.:xmlns:Common="clr-namespace:Fox.UI.Resource;assembly=Fox.UI.Resource"xmlns:local="clr-namespace:LSE" what is LSE, if you write that you use namespace MyNameSpace? Use MyNameSpace instead of LSE 这篇关于WPF命名空间斗争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 13:07