【问题标题】:WPF application throws an 'XmlParseException was unhandled' when using ConverterWPF 应用程序在使用 Converter 时抛出“未处理 XmlParseException”
【发布时间】: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; }
    }

我不知道为什么它不起作用,而且我对这个程序有点沮丧。 任何帮助都将非常感激

【问题讨论】:

    标签: wpf image converter


    【解决方案1】:

    使用&lt;local:imageconverter x:Key="Convert"/&gt;

    【讨论】:

    • 谢谢,有帮助。原来问题是我把 imageconverter 类放在另一个叫做转换器的类中。但我要盲代码才能看到它。你的建议让我走上了错误的轨道。
    【解决方案2】:

    您的转换器需要实现IValueConverter 接口,否则 WPF 将不知道如何处理它(因此它会为您提供该异常。)

    class Converter : IValueConverter
    {
       ...
    }
    

    【讨论】:

    • 感谢您的帮助,但这不是问题所在。当我复制代码时,我不知何故设法按下了使所有内容都变成小写字母的组合键。
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 2014-06-16
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多