Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
来自web的WPF图像替代源_Wpf_Image_Binding - Fatal编程技术网

来自web的WPF图像替代源

来自web的WPF图像替代源,wpf,image,binding,Wpf,Image,Binding,我将图像存储到websevers中。如果第一台服务器不可用,我希望有另一个加载图像的路径 比如: <Image Source="http://server1/images/image1" AlternativeSource="http://server2/images/image1"/> 如何操作?Lucky for us Image control在加载图像失败时会引发一个事件,因此很容易检查图像加载是否失败 可以采取的一种方法是扩展图像控件 publ

我将图像存储到websevers中。如果第一台服务器不可用,我希望有另一个加载图像的路径

比如:

   <Image Source="http://server1/images/image1" 
          AlternativeSource="http://server2/images/image1"/>

如何操作?

Lucky for us Image control在加载图像失败时会引发一个事件,因此很容易检查图像加载是否失败

可以采取的一种方法是扩展图像控件

public class AlternativeImage : Image
{

    private bool _tryAlternativeSource ;

    public string AlternativeSource
    {
        get { return (string)GetValue(AlternativeSourceProperty); }
        set { SetValue(AlternativeSourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ItemTemplate.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AlternativeSourceProperty =
        DependencyProperty.Register("AlternativeSource", typeof(string), typeof(AlternativeImage), new PropertyMetadata(null));

    public AlternativeImage()
    {
        Initialized += OnInitialized;
    }

    private void OnInitialized(object sender, EventArgs eventArgs)
    {
        _tryAlternativeSource = !string.IsNullOrEmpty(AlternativeSource);

        //Note , ths need to be unregistered 
        ImageFailed += OnImageFailed;
    }

    private void OnImageFailed(object sender, ExceptionRoutedEventArgs exceptionRoutedEventArgs)
    {
        if (!_tryAlternativeSource)
            return;

        _tryAlternativeSource = false;

        Source = new ImageSourceConverter().ConvertFromString(AlternativeSource) as ImageSource;
    }
}
然后像这样使用它

<controls:AlternativeImage Source="http://server1/images/image1" 
      AlternativeSource="https://media.licdn.com/mpr/mpr/p/1/005/07f/0e1/07ab226.jpg"/>
上传了完整的代码