2011-07-01 30 views
0

我正在构建一个小的Wpf应用程序来学习我自己的wpf。 我遇到了一个控制者的问题。 我有一个url列表的字符串格式的对象,我想将它们绑定到图像,并使用wpf转换器类将url转换为位图。当使用转换器时,WPF应用程序抛出'XmlParseException was unhandled'

但是,当我实现转换的程序引发以下错误:

'XmlParseException was unhandled'

而且在细节上也这样说:

"{"Unable to cast object of type 'ChanGrabber.Converter' to type '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; } 
    } 

我不知道为什么它赢得”工作,我对这个计划感到沮丧。 任何帮助将令人难以置信的如此赞赏

回答

1

使用<local:imageconverter x:Key="Convert"/>

+1

谢谢,这有帮助。 原来问题是我把imageconverter类放在另一个叫做converter的类中。 但我是盲目地看到它。 你的建议让我走上了错误的轨道。 –

0

你的转换器需要实现IValueConverter接口,否则将WPF不知道该怎么用它做(所以它为您提供了异常。)

class Converter : IValueConverter 
{ 
    ... 
} 
+0

感谢您的帮助,但那不是问题。 我不知何故设法按下组合键,当我复制代码时,将所有东西都变成小写字母。 –

相关问题