【问题标题】:WPF animated GIFWPF 动画 GIF
【发布时间】:2016-02-19 04:32:32
【问题描述】:

我正在尝试使用来自 https://github.com/XamlAnimatedGif/WpfAnimatedGif/ 的库来使用 wpf 解决我的动画 gif。我写了一些代码将图像绑定到图像,但它不起作用。

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.

谢谢。

【问题讨论】:

  • 大家好,我在我的问题中添加了错误部分,tq。
  • 看起来您的图像 Uri 已损坏。确保图像位于相对于您的 exe 位置的“图像”文件夹中(类似于 WpfAnimation\bin\Debug\Images\anipier_e0.gif)。

标签: c# wpf


【解决方案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 是否与您的视图相关联。

编辑:

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

【讨论】:

  • 动画 gif 在资源中编译。
  • 检查构造函数。加载 gif 时出现异常。
猜你喜欢
  • 2013-11-10
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
相关资源
最近更新 更多