【发布时间】:2011-09-26 17:55:48
【问题描述】:
我正在构建一个小型 Wpf 应用程序来学习自己的 wpf。 我遇到了其中一个控制器的问题。 我有一个带有字符串格式的 url 列表的对象,我想将它们绑定到图像并使用 wpf 转换器类将 url 转换为位图。
但是当我实现转换器时,程序会抛出以下错误:
'XmlParseException 未处理'
在细节中它是这样说的:
"{"无法转换类型的对象 'ChanGrabber.Converter' 输入 'System.Windows.Data.IValueConverter'。"}"
这是在 xaml 中引用转换器的代码:
xmlns:local="clr-namespace:ChanGrabber">
<Window.Resources>
<local:Converter x:Key="Convert"/>
</Window.Resources>
这是我使用控件的代码:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ThumbImgUrl, Converter={StaticResource Convert}}" />
</StackPanel>
</DataTemplate>
这是转换器的代码:
namespace ChanGrabber
{
class Converter
{
[valueconversion(typeof(string), typeof(bitmapimage))]
public class imageconverter : ivalueconverter
{
public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)
{
try
{
string mypath = (string)value;
uri myuri = new uri(mypath);
bitmapimage animage = new bitmapimage(myuri);
return animage;
}
catch (exception)
{
return new bitmapimage(new uri("ikke funket"));
}
}
public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)
{
throw new notimplementedexception();
}
}
这是我绑定到图像的对象
class MainPosts : MainLinks
{
public MainPosts(string _title, string _link, String _postText, string _imageUrl, string _thumbUrl) :base(_title,_link)
{
PostText = _postText;
ImageUrl = _imageUrl;
ThumbImgUrl = _thumbUrl;
}
public String PostText { get; set; }
public String ImageUrl { get; set; }
public string ThumbImgUrl { get; set; }
}
我不知道为什么它不起作用,而且我对这个程序有点沮丧。 任何帮助都将非常感激
【问题讨论】: