WPF动画GIF

时间:2012-10-06 07:20:45

标签: c# wpf

我正在尝试使用https://github.com/XamlAnimatedGif/WpfAnimatedGif/中的库来使用wpf解析我的动画gif。我写了一些代码将图像绑定到Image,但是它没有用。

WPF:

<Image gif:ImageBehavior.RepeatBehavior="Forever"
           gif:ImageBehavior.AnimatedSource="{Binding ElementName=WpfAnimation, Path=ImageStatus}"/>

代码背后:

// Image status property
        public readonly DependencyProperty ImageStatusProperty;
    public ImageSource ImageStatus
    {
        get { return (ImageSource)GetValue(ImageStatusProperty); }
        set { SetValue(ImageStatusProperty, value); }
    }

    public MainWindow()
    {
        InitializeComponent();
        ImageStatus = new BitmapImage(new Uri("Images/anipier_e0.gif", UriKind.Relative));

    }

错误:

System.Windows.Markup.XamlParseException was unhandled
  HResult=-2146233087
  Message='The invocation of the constructor on type 'WpfAnimation.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.
  Source=PresentationFramework
InnerException: System.ArgumentNullException
       HResult=-2147467261
       Message=Value cannot be null.

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为你的绑定有问题:AnimatedSource绑定到名为“WpfAnimation”的元素。您应该将AnimatedSource绑定到DataContext的属性。如果WpfAnimation与LayoutRoot类似,请使用如下表达式:

<Image gif:ImageBehavior.RepeatBehavior="Forever"
           gif:ImageBehavior.AnimatedSource="{Binding ElementName=WpfAnimation, Path=DataContext.ImageStatus}"/>

另外,检查Debug / Output是否存在绑定错误,动画gif是资源,Uri是否有效,以及DataContext是否与视图相关联。

修改

看起来视图构造函数中存在异常。