WPF 应用程序抛出“XmlParseException 未处理”使用转换器时
我正在构建一个小型 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; }
}
我不知道为什么它不起作用,而且我对这个程序有点沮丧。 任何帮助将非常感激
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();
}
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
use
<local:imageconverter x:Key="Convert"/>
您的转换器需要实现 IValueConverter 接口,否则 WPF 将不知道如何处理它(因此它会给您该异常。)
Your converter needs to implement the
IValueConverter
interface, otherwise WPF won't know what to do with it (so it gives you that exception.)