Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从其他程序集加载图像时显示的图像_C#_Wpf_Mvvm_Visual Studio 2013_Bitmapimage - Fatal编程技术网

C# 从其他程序集加载图像时显示的图像

C# 从其他程序集加载图像时显示的图像,c#,wpf,mvvm,visual-studio-2013,bitmapimage,C#,Wpf,Mvvm,Visual Studio 2013,Bitmapimage,我正在使用Visual Studio 2013 WPF和MVVM模式。 我有一个模块化的解决方案,在一个项目解决方案中我有很多项目 公共库(类库) 这基本上是我的库,它将包含所有的基本内容,例如类ViewModelBase.cs实现INotifyPropertyChanged。我还有一个包含图像(jpg、png等)的Images文件夹和另一个名为BinaryImageConverter.cs的类,用于将二进制图像转换为位图图像 客户(WPF项目) 在这里,我有一个用户控件,其中有一个图像控件。

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

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

  • 公共库(类库)

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

  • 客户(WPF项目)

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

CustomerModel:ViewModelBase

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字段将图像存储在数据库中

因此,我将数据加载到CustomerServiceWModel中。除我的用户控件上的图像外,所有内容都显示出来。 我有显示的名字和姓氏,这是完美的,但图像没有出现

编辑: 忘了提到我的用户控件加载在一个列表框中,ItemSource绑定到我的CustomerProfile,这是CustomerModel的一个可见集合

<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>

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

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

为什么不直接从字节数组创建MemoryStream,比如
新建MemoryStream(Picture)
,从而避免写入和查找?
BinaryPictureConverter
是否在任何地方使用?您的
byte[]
数组是编码图像(例如,以PNG、JPG或BMP等图像格式),还是原始位图数据?
BinaryPictureConverter
从DB接收图像(我将图像存储在客户表中的varbinary字段中),查看客户时,它会将varbinary更改为BitmapImage以便我可以显示它,现在我的问题是,当表中的varbinary字段为null时,我需要显示一个默认图像,它位于公共.Library程序集中,我尝试使用FallbackValue和TargetNullValue,但没有成功,我没有得到任何Xaml或运行时错误,当我插入FallbackValue时,我甚至可以看到图片,但在运行时,没有为没有图像的客户显示任何内容。哇,我是一个IDOT没有看到这一点,感谢Clemens,是的,当我从Customer表加载数据时,会使用BinaryPictureConverter,即我提到的varbinary字段。谢谢guysAlso,看来TargetNullValue不起作用了,我现在可以看到我的客户的图像,但是没有任何图像的图像不会加载它应该加载的DefaultCustomerImage。。。为什么?对于某些客户,
CustomerProfile
null?如果是这样,绑定将生成一个错误(null引用),而不是
null
,因此您应该同时设置
TargetNullValue
FallbackValue
(当绑定失败/出错时应用)。实际上CustomerProfile从不为null CustomerProfile.ProfilePicture为null,如果为null,则他应该去获取DefaultCustomerImage.jpg。我已尝试放置Fallback值,但仍然没有结果。如果他在属性中发现null,则我认为TargetNullValue应该获取默认图像…属性中有任何绑定错误吗输出窗口?
DefaultCustomerImage.jpg
是否具有资源或内容的生成操作(必须是其中之一才能使
pack
uri正常工作)?如果是内容,您是否将其设置为复制到输出目录?你能直接显示图像吗(不需要绑定)?