转换器返回不同类型?

发布于 2025-01-07 17:49:06 字数 1993 浏览 3 评论 0原文

我有一个 List。每个Employee都有一个byte[]存储一张图片(或null)。我需要以某种方式将此字节数组绑定到我正在使用的内容模板上的图像控件,或者如果员工没有图片,我想显示本地 jpg 文件。我想出的方法是定义一个转换器,它将返回 BitmapImage(GetImageFromByteArray() 方法的返回类型)或字符串(文件名的路径)。这显然意味着该方法能够返回两种类型,但我不会认为这是一个问题,因为我已将返回类型指定为对象。

无论如何,这是我的 C#:

public class GuidToImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Guid id = new Guid(value.ToString());
                Employee employee = Employees.SingleOrDefault(o => o.Id.Equals(id));
                return employee.Picture != null ? GetImageFromByteArray(employee.Picture) : "/Resource/images/silhouette.jpg";
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException();
            }
    }

并在 XAML 中使用,如下所示:

    <local:GuidToImageConverter x:Key="GuidToImageConverter"/>

    <local:OrientedGroupHeaderContentTemplateSelector x:Key="GroupHeaderContentTemplateSelector">
        <!-- Default templates: -->
        <local:OrientedGroupHeaderContentTemplateSelector.HorizontalMonthViewDateTemplate>
            <DataTemplate>
                <Image Width="60" Height="60" Margin="5 0 10 0" HorizontalAlignment="Left" Stretch="UniformToFill" Source="{Binding Path=Name.Id, Converter={StaticResource GuidToImageConverter}, ConverterParameter=1}" />
            </DataTemplate>
        </local:OrientedGroupHeaderContentTemplateSelector.HorizontalMonthViewDateTemplate>
    </local:OrientedGroupHeaderContentTemplateSelector>

错误:

“错误 1 ​​- 无法确定条件表达式的类型,因为“System.Windows.Media.Imaging.BitmapImage”和“string”之间没有隐式转换“

我知道,如果有适当的 MVVM 结构,这可能不会成为问题,但目前要改变这一切是不可行的。

I have a List<Employee>. Each Employee has a byte[] storing a picture (or null). I need to somehow bind this byte array to an image control on a content template I'm using, or if the Employee doesn't have a picture I want to display a local jpg file. The way I've come up with is to define a converter that will return either the BitmapImage (return type from GetImageFromByteArray() method) or a string (the path of the filename). This obviously means that this method is capable of returning two types, but I wouldn't have thought this was a problem seeing as though I've specified the return type as object.

Anyway, here's my C#:

public class GuidToImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Guid id = new Guid(value.ToString());
                Employee employee = Employees.SingleOrDefault(o => o.Id.Equals(id));
                return employee.Picture != null ? GetImageFromByteArray(employee.Picture) : "/Resource/images/silhouette.jpg";
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException();
            }
    }

And used in the XAML like so:

    <local:GuidToImageConverter x:Key="GuidToImageConverter"/>

    <local:OrientedGroupHeaderContentTemplateSelector x:Key="GroupHeaderContentTemplateSelector">
        <!-- Default templates: -->
        <local:OrientedGroupHeaderContentTemplateSelector.HorizontalMonthViewDateTemplate>
            <DataTemplate>
                <Image Width="60" Height="60" Margin="5 0 10 0" HorizontalAlignment="Left" Stretch="UniformToFill" Source="{Binding Path=Name.Id, Converter={StaticResource GuidToImageConverter}, ConverterParameter=1}" />
            </DataTemplate>
        </local:OrientedGroupHeaderContentTemplateSelector.HorizontalMonthViewDateTemplate>
    </local:OrientedGroupHeaderContentTemplateSelector>

The error:

"Error 1 - Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Windows.Media.Imaging.BitmapImage' and 'string'"

I understand that this would probably not be an issue if a proper MVVM structure was in place but it's not feasible at the moment to change it all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2025-01-14 17:49:06

将 return 语句更改为以下内容以使其正常工作:

if(employee.Picture != null)
    return GetImageFromByteArray(employee.Picture)
return "/Resource/images/silhouette.jpg";

或者您可以首先从“默认图像”创建图像,然后可以像以前一样使用 return 语句。

Change the return statement to this to make it work:

if(employee.Picture != null)
    return GetImageFromByteArray(employee.Picture)
return "/Resource/images/silhouette.jpg";

Or you could first create an image from the 'default image' and then you can use the return statement as before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文