WPF图像后备不适用于失败的Web图像 [英] WPF image fallback doesn't work on failed web images

查看:56
本文介绍了WPF图像后备不适用于失败的Web图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a control that contains an image that is supposed to be pulled from a file location (either web or local). I have the following XAML code:

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

这是它所指的img_Fallback:

and this is the img_Fallback that it refers to:

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

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

When the relevant property is a null or empty string it shows up just fine. And as expected if the property is set and the file exists the main image shows up. However when the property is set to a location that isn't found nothing shows up at all, neither the main image (obviously) or the fallback. How do you make the fallback show up in this case?

推荐答案

这可能是过分的了-但您可以使用行为来解决.

This might be overkill - but you could solve this with a Behaviour.

XAML

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

如果找不到 http://schemas.microsoft.com/expression/2010/interactivity 命名空间,请从Visual Studio中的 Assemblys 中添加它.

If you dont find the http://schemas.microsoft.com/expression/2010/interactivitynamespace - add it from your Assemblys in your Visual Studio.

    <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));
            }
        }
    }

其他 信息

如何通过编程方式设置 Source Image .

How to set the Source an Image programmaticly.

这篇关于WPF图像后备不适用于失败的Web图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆