【问题标题】:Image to showing when Loading Image from antoher Assembly从另一个程序集加载图像时显示的图像
【发布时间】:2014-10-23 18:35:24
【问题描述】:

我正在使用 Visual Studio 2013 WPF 和 MVVM 模式。

我有一个模块化解决方案,在 1 个项目解决方案中我有很多项目。

  • Common.Library(类库)

    这基本上是我的库,它将包含所有基本内容,例如 ViewModelBase.cs 类实现 INotifyPropertyChanged。我还有一个包含图像(jpg、png 等)的 Images 文件夹和另一个名为 BinaryImageConverter.cs 的类,用于将二进制图像转换为 BitmapImages

  • 客户(WPF 项目)

    在这里,我有一个用户控件,其中包含一个图像控件。我得到了我的模型/视图/视图模型

客户模型:视图模型库

private BitmapImage _ProfilePicture;

private BitmapImage ProfilePicture
    {
        get { return this._ProfilePicture; }
        set
        {
            if (this._ProfilePicture == value)
                return;

            this._ProfilePicture = value;
            OnPropertyChanged("ProfilePicture");
        }
    }

BinaryImageConverter.cs

public BitmapImage BinaryPictureConverter(byte[] Picture)
    {
        BitmapImage Image;

        if (Picture == null)
            return null;

        Image = new BitmapImage();

        using (MemoryStream imageStream = new MemoryStream())
        {
            imageStream.Write(Picture, 0, Picture.Length);
            imageStream.Seek(0, SeekOrigin.Begin);

            Image.BeginInit();
            Image.CacheOption = BitmapCacheOption.OnLoad;
            Image.StreamSource = imageStream;
            Image.EndInit();
            Image.Freeze();
        }

        return Image;
    }

CustomerView.xaml

<UserControl.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="DefaultCustomerPicture" UriSource="/Common.Library;component/Images/DefaultCustomerPicture.jpg" />
    </ResourceDictionary>
</UserControl.Resources>

<Image Name="ImgProfile" Width="150" HorizontalAlignment="Left" DockPanel.Dock="Left" Source="{Binding Path=CustomerProfile.ProfilePicture, TargetNullValue={StaticResource DefaultCustomerPicture}}" Stretch="Fill" />

我有一个 varbinary 字段来将我的图像存储在数据库中。

所以我将我的数据加载到我的 CustomerViewModel 中。一切都显示除了我的用户控件上的图像。 我显示了完美的名字和姓氏,但图像没有出现。

编辑: 忘了提到我的用户控件加载在 ListBox 中,并且 ItemSource 绑定到我的 CustomerProfile,它是 CustomerModel 的 ObservableCollection

<ListBox Name="listCustomers" Background="Transparent" BorderThickness="0" Margin="5,10,0,0" ItemsSource="{Binding Path=CustomerProfile}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <vw:CustomerView />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

  • 为什么不直接从字节数组创建 MemoryStream,比如new MemoryStream(Picture),从而避免了 Write and Seek? BinaryPictureConverter 是否在任何地方使用过?
  • 您的byte[] 数组是编码图像(例如,PNG、JPG 或 BMP 等图像格式),还是原始位图数据?
  • BinaryPictureConverter 从数据库接收图片(我将图像存储在客户表中的 varbinary 字段中),查看客户时,它将 varbinary 更改为 BitmapImage,以便我可以显示它,现在我的问题是当表中的 varbinary 字段为空时,我需要显示默认图像,它位于 Common.Library 程序集中,我尝试使用 FallbackValue 和 TargetNullValue 但没有成功我没有得到任何Xaml 或运行时错误,当我插入 FallbackValue 时,我什至可以看到图片,但在运行时对于没有图片的客户没有任何显示。

标签: c# wpf mvvm visual-studio-2013 bitmapimage


【解决方案1】:

WPF 数据绑定仅适用于 public 属性:

public BitmapImage ProfilePicture
{
    get { ... }
    set { ... }
}

【讨论】:

  • 哇我是一个 IDOT 没有看到这一点,谢谢 Clemens,是的,当我从客户表加载我的数据时使用了 BinaryPictureConverter,我谈到了 varbinary 字段。谢谢大家
  • 另外,TargetNullValue 似乎不起作用,我现在可以看到我的客户图片,但没有任何客户的图片没有加载它应该加载的 DefaultCustomerImage。 ..这是为什么?
  • 对于某些客户,CustomerProfile 是否为空?如果是这样,绑定会产生错误(空引用)而不是null,因此您应该同时设置TargetNullValueFallbackValue(在绑定失败/故障时应用)。
  • 实际上 CustomerProfile 永远不会为空 CustomerProfile.ProfilePicture 为空,如果它为空,他应该去获取 DefaultCustomerImage.jpg 我已经尝试放置 Fallbackvalue 但仍然没有我想 TargetNullValue 是假设如果他在属性中找到 null,则获取默认 Image ...
  • 输出窗口中是否有任何绑定错误? DefaultCustomerImage.jpg 是否具有资源或内容的构建操作(pack URI 必须是其中一个才能工作)?如果是内容,您是否将其设置为复制到输出目录?可以直接显示图片吗(不用绑定)?
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 2018-10-29
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
相关资源
最近更新 更多