【问题标题】:WPF image fallback doesn't work on failed web imagesWPF 图像回退不适用于失败的 Web 图像
【发布时间】:2017-06-01 20:21:05
【问题描述】:

我有一个控件,其中包含应该从文件位置(网络或本地)提取的图像。我有以下 XAML 代码:

        <Image Width="96" Height="96" Stretch="Uniform" Grid.Column="1"
               Source="{Binding PreviewImageFullPath, FallbackValue={StaticResource img_Fallback}, TargetNullValue={StaticResource img_Fallback}}" />

这就是它所指的 img_Fallback:

<BitmapImage UriSource="pack://application:,,,/GamutBase;component/Images/Icon_PreviewMissing.png" x:Key="img_Fallback" />

当相关属性为 null 或空字符串时,它显示得很好。正如预期的那样,如果设置了属性并且文件存在,则会显示主图像。但是,当属性设置为未找到的位置时,根本不会显示任何内容,主图像(显然)或后备图像都不会显示。在这种情况下,您如何使后备出现?

【问题讨论】:

  • 请注意,FallBackValue 是绑定本身的属性。这意味着,当绑定失败时,将使用 FallBackValue。但是绑定本身并没有失败,对吧? Image 有一个 ImageFailed 事件,您可以使用它来检测无法从网络加载图像...
  • ImageFailed 事件完美运行,我只是告诉它在图像失败时返回 null 并触发属性更改事件,就像我希望的那样更新!如果你把它作为一个答案,我会接受。
  • 您还可以实现一个绑定转换器,它显式读取图像文件或发出 http 请求,并在无法加载原始图像时返回备用图像。
  • 其实我是这么想的,只是为了一个非常简单的事情设置了很多代码。我喜欢图像控件自己处理背景加载的方式,如果我没记错的话,我必须自己实现它。
  • 随意标记我的答案

标签: wpf image xaml


【解决方案1】:

这可能有点矫枉过正 - 但您可以使用 Behaviour 解决此问题。

XAML

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:myBehaviour="clr-namespace:MyWpfApp.Behaviours"

如果您没有找到 http://schemas.microsoft.com/expression/2010/interactivitynamespace - 从 Visual Studio 中的 Assemblys 添加它。

    <Image Width="96" 
           Height="96" 
           Stretch="Uniform" 
           Grid.Column="1"
           Source="{Binding PreviewImageFullPath}">
        <i:Interaction.Behaviors>
            <myBehaviour:WebImageFallBackBehaviour FallBackImageSource="{StaticResource img_Fallback}" />
        </i:Interaction.Behaviors>
    </Image>

C#

    public class WebImageFallBackBehaviour : System.Windows.Interactivity.Behavior<Image>
    {


        public string FallBackImageSource
        {
            get { return (string)GetValue(FallBackImageSourceProperty); }
            set { SetValue(FallBackImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FallBackImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FallBackImageSourceProperty =
            DependencyProperty.Register("FallBackImageSource", typeof(string), typeof(WebImageFallBackBehaviour), new PropertyMetadata(string.Empty));



        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.ImageFailed += AssociatedObject_ImageFailed;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.ImageFailed -= AssociatedObject_ImageFailed;
        }

        private void AssociatedObject_ImageFailed(object sender, System.Windows.ExceptionRoutedEventArgs e)
        {
            Image self = sender as Image;

            if(self != null)
            {
                self.Source = new BitmapImage(new Uri(FallBackImageSource, UriKind.Relative));
            }
        }
    }

其他 信息

如何以编程方式设置SourceImage

【讨论】:

  • 哇,这是相当多的代码,谢谢!在这种情况下,我将处理图像失败事件,但要为此付出努力......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多