我使用带有Update 1的Visual Studio 2015社区和Windows 8.1和Windows Phone 8.0 / 8.1工具'在Windows 10 Pro上安装组件。我使用针对Windows Phone 8.1的Blank Windows Phone模板创建了一个项目。我关注MVVM tutorial on MSDN,并且我使用IValueConverter类来查看问题。这是我到目前为止所做的:
创建了我的IValueConverter类:
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();
}
}
}
请注意,该类已在我的主项目中创建,位于名称空间Realgia.WindowsPhone
中。另请注意,我使用的界面是Windows.UI.Xaml.Data.IValueConverter
,而不是教程中使用的System.Windows.Data.IValueConverter
(我相信教程已经过时了?)
在我的UserControl的XAML中,我为包含ValueConverter的命名空间添加了一个xmlns:
xmlns:converters="clr-namespace:Realgia.WindowsPhone"
现在我尝试将转换器作为资源添加到同一XAML文件中的UserControl。正如您在下面看到的那样,intellisense实际上会选择我的ValueConverterDate类:
所以完整的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>
我在编译时遇到以下错误:
未知类型&#39; ValueConverterDate&#39;在XML命名空间中 &#39; CLR-名称空间:Realgia.WindowsPhone;装配= Realgia.WindowsPhone, Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39;
我真的很难理解为什么XAML解析器无法找到这个类,因为它被intellisense选中,这个calss的命名空间肯定是Realgia.WindowsPhone(你可以从类定义中看到)我的xmlns指向这个命名空间。
我尝试过以下方法:
<converters:ValueConverterDate x:Key="ValueConverterDate" />
位代码,编译(编译成功),然后重新添加代码并尝试再次编译xmlns:converters="using:Realgia.WindowsPhone"
代替xmlns:converters="clr-namespace:Realgia.WindowsPhone"
编辑看到评论,解决方案实际上证明是用的xmlns:converters="using:Realgia.WindowsPhone"
代替xmlns:converters="clr-namespace:Realgia.WindowsPhone"
我确定我曾经尝试过一次并且它没有工作,不确定我是第一次犯了错误还是我做了别的事情并且修复了#39;这个(例如关闭解决方案,重启我的电脑)
我开始怀疑问题可能是因为我使用Windows.UI.Xaml.Data.IValueConverter
而不是System.Windows.Data.IValueConverter
,但System.Windows.Data.IValueConverter
是汇编PresentationFramework.dll,并且我无法使用Visual Studio中的搜索在任何地方找到此程序集&#39; Add Reference&#39;。如果有人可以解释这个的历史(为什么两个不同的程序集中有两个IValueConverter接口?)也许这会有所帮助 - 它在文档中没有提到。两者之间只有一点点差别(一个采用字符串参数,另一个采用CultureInfo参数)。
有什么想法吗?
答案 0 :(得分:0)
我只是给了它一个旋转,一个WP8.1应用程序(非silverlight)需要&#39; using
&#39; xmlns语法并使用Windows.UI.Xaml.Data.IValueConverter
。如果您创建了WP8.1 silverlight应用程序,则应使用clr-namespace:xmlns语法并实现System.Windows.Data.IValueConverter
。鉴于您只有第一个valueconverter,我说您需要坚持使用using
语法。还要仔细检查你没有遇到智能感知故障,我之前看过WPF项目的这类错误,但它们只是由intellisense生成,实际编译工作正常(检查&#39)输出&#39;窗格)。