即使正确键入了 xmlns,也无法在 XAML 中引用 ValueConverter class

Can't reference ValueConverter class in XAML even with xmlns correctly typed

我在 Windows 10 Pro 上使用 Visual Studio 2015 Community with Update 1 和 'Windows 8.1 and Windows Phone 8.0/8.1 Tools' 安装组件。我使用针对 Windows Phone 8.1 的空白 Windows Phone 模板创建了一个项目。我正在关注 MVVM tutorial on MSDN, and I've hit a problem using an IValueConverter class 进行查看。这是我到目前为止所做的:

创建了我的 IValueConverter class:

namespace Realgia.WindowsPhone
{
    public class ValueConverterDate : IValueConverter
    {
        // This converts the DateTime object to the string to display.
        public object Convert(object value, Type targetType,
            object parameter, string language)
        {
            // Retrieve the format string and use it to format the value.
            string formatString = parameter as string;
            if (!string.IsNullOrEmpty(formatString))
            {
                return string.Format(
                    new CultureInfo(language), formatString, value);
            }
            // If the format string is null or empty, simply call ToString()
            // on the value.
            return value.ToString();
        }

        // No need to implement converting back on a one-way binding 
        public object ConvertBack(object value, Type targetType,
            object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}

请注意,class 已在我的主项目中的命名空间 Realgia.WindowsPhone 中创建。另请注意,我使用的界面是 Windows.UI.Xaml.Data.IValueConverter,而不是教程中使用的 System.Windows.Data.IValueConverter(我认为教程已过时?)

在我的用户控件的 XAML 中,我为包含 ValueConverter 的命名空间添加了一个 xmlns:

xmlns:converters="clr-namespace:Realgia.WindowsPhone"

现在我正尝试将转换器作为资源添加到同一个 XAML 文件中的 UserControl。正如您在下面看到的,intellisense 实际上选择了我的 ValueConverterDate class:

所以完整的 XAML 代码是:

<UserControl
    x:Class="Realgia.WindowsPhone.View.MeasurementsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converters="clr-namespace:Realgia.WindowsPhone"

    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <UserControl.Resources>
        <converters:ValueConverterDate x:Key="ValueConverterDate" />
    </UserControl.Resources>

    <Grid>
    </Grid>
</UserControl>

我在编译时不断收到以下错误:

Unknown type 'ValueConverterDate' in XML namespace 'clr-namespace:Realgia.WindowsPhone;assembly=Realgia.WindowsPhone, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

我真的很难理解为什么XAML解析器找不到这个class,因为它被intellisense捡到了,这个calss的命名空间肯定是Realgia.WindowsPhone (正如您从 class 定义中看到的那样)并且我的 xmlns 指向此命名空间。

我试过以下方法:

EDIT See comments, the solution in fact turned out to be to use xmlns:converters="using:Realgia.WindowsPhone" instead of xmlns:converters="clr-namespace:Realgia.WindowsPhone" I'm sure I tried this once and it didn't work, not sure if I made a mistake the first time or whether something else I did 'fixed' this (e.g. closing the solution, rebooting my computer)

我开始怀疑这个问题可能是因为我使用的是 Windows.UI.Xaml.Data.IValueConverter,而不是 System.Windows.Data.IValueConverter,但是 System.Windows.Data.IValueConverter 在汇编 PresentationFramework.dll 中,并且我在 Visual Studio 'Add Reference' 中使用搜索在任何地方都找不到这个程序集。如果有人能解释这件事的历史(为什么在两个不同的程序集中有两个 IValueConverter 接口?)也许会有帮助——文档中没有提到。两者之间只有细微差别(一个接受字符串参数,另一个接受 CultureInfo 参数)。

有什么想法吗?

我只是试了一下,WP8.1 应用程序(非 silverlight)需要“using”xmlns 语法并使用 Windows.UI.Xaml.Data.IValueConverter。如果您创建了一个 WP8.1 silverlight 应用程序,您应该使用 clr-namespace:xmlns 语法并实现 System.Windows.Data.IValueConverter。鉴于您只有第一个值转换器,我会说您需要坚持使用 using 语法。还要仔细检查你是否 运行 进入智能感知故障,我以前在 WPF 项目中看到过这些类型的错误,但它们仅由智能感知生成,实际编译工作正常(检查 'output'窗格)。