WPF application throws an 'XmlParseException was unhandled' when using Converter
I am building a small Wpf aplication to learn myself wpf. And i have encountered a problem with one of the controlers. i Have an object with a list of url's in a string format, and i want to bind them to an image and use the wpf converter class to convert the url's to bitmaps.
But when i implement the converter the program throws the following error:
'XmlParseException was unhandled'
And in the details it says this:
"{"Unable to cast object of type 'ChanGrabber.Converter' to type 'System.Windows.Data.IValueConverter'."}"
This is the code for referencing the converter in the xaml:
xmlns:local="clr-namespace:ChanGrabber">
<Window.Resources>
<local:Converter x:Key="Convert"/>
</Window.Resources>
This is the code where i use the control:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ThumbImgUrl, Converter={StaticResource Convert}}" />
</StackPanel>
</DataTemplate>
and here is the code for the converter:
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开发者_StackOverflow();
}
}
And this is is the object i am binding to the image
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; }
}
I have no idea why it won't work, and i am getting abit frustrated on the program. Any help will be so incredibly appreciated
use <local:imageconverter x:Key="Convert"/>
Your converter needs to implement the IValueConverter
interface, otherwise WPF won't know what to do with it (so it gives you that exception.)
class Converter : IValueConverter
{
...
}
精彩评论