为WPF图像类提供初始图像占位符

为WPF图像类提供初始图像占位符,wpf,wpf-controls,binding,styles,wpftoolkit,Wpf,Wpf Controls,Binding,Styles,Wpftoolkit,运行项目时出现运行时错误ocure: 错误:必须设置属性“UriSource”或属性“StreamSource”。 因为this.ImageUri为null,我不知道为什么this.ImageUri为null!帮帮我 我一直在使用WPF列表框,使用图像作为列表框项。源映像路径指向承载这些映像的服务器。在快速网络上,图像显示没有明显延迟。然而,通过一个缓慢的链接,用户体验明显下降,我真的很想在下载和解码图像时显示一个占位符图像 令人惊讶的是,我在博客圈中没有找到解决这个问题的方法,所以我编写了一个

运行项目时出现运行时错误ocure: 错误:必须设置属性“UriSource”或属性“StreamSource”。 因为this.ImageUri为null,我不知道为什么this.ImageUri为null!帮帮我

我一直在使用WPF列表框,使用图像作为列表框项。源映像路径指向承载这些映像的服务器。在快速网络上,图像显示没有明显延迟。然而,通过一个缓慢的链接,用户体验明显下降,我真的很想在下载和解码图像时显示一个占位符图像

令人惊讶的是,我在博客圈中没有找到解决这个问题的方法,所以我编写了一个派生类来解决这个问题

下面的示例XAML来自我的项目容器样式。我用本地类实现local:ImageLoader替换了Image

<Window.Resources>
<DataTemplate DataType="{x:Type local:MyData}">
...
<StackPanel Grid.Column="0" Margin="5">
<Border BorderThickness="0">
<MyControl:ImageLoader Width="50" Height="50" ImageUri="{Binding Path=profile_image_url_https, FallbackValue=profile_image_url_https}" InitialImage="/MyProject;component/Images/nopic.png"  HorizontalAlignment="Left"></imgz:ImageLoader>
</Border>
</StackPanel>
...
</DataTemplate>
</Window.Resources>

<Grid>
<ListBox ItemsSource="{Binding Source = {StaticResource MyData}}"    />
</Grid>
运行项目时出现运行时错误: 错误:必须设置属性“UriSource”或属性“StreamSource”。
因为this.ImageUri为null,我不知道为什么this.ImageUri为null!如果不是InitialImage=“/MyProject;component/Images/nopic.png”中的分号输入错误,请帮助我,
也许最好在ImageUri中将InitialImage设置为默认值

 public static readonly DependencyProperty ImageUriProperty = DependencyProperty.Register(
    "ImageUri", typeof(Uri), typeof(ImageLoader), new PropertyMetadata(new Uri("/MyProject/component/Images/nopic.png"), null));

更新:

您必须绑定到
Image.Source
,并且可以使用
PriorityBinding
显示占位符

<Image.Source>
    <PriorityBinding>
        <!--highest priority sources are first in the list-->
        <Binding Path="YourImageUri"
           IsAsync="True" />
        <Binding Path="InitialImageUri"
           IsAsync="True" />
    </PriorityBinding>
</Image.Source>

对于“LoadFailedImage”,a将订阅到
Image.ImageFailed
事件。


希望这有帮助。

不起作用,问题出在这个上。loadeImage.UriSource=this.ImageUri;行,ImageUri不正常。见中的备注。“属性更改只能在对象初始化期间发生”。我不相信绑定知道:)我测试了它,但InitialImage很快就死掉了!我不知道为什么YourImageUri的绑定源成功解析,但尚未完成加载。hmmmm然后我将绑定到一个属性,该属性返回的不是Uri而是加载的图像。
<Image.Source>
    <PriorityBinding>
        <!--highest priority sources are first in the list-->
        <Binding Path="YourImageUri"
           IsAsync="True" />
        <Binding Path="InitialImageUri"
           IsAsync="True" />
    </PriorityBinding>
</Image.Source>