You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决WPF中无法加载XamlAnimatedGif程序集的异常?

问题描述

我一直在寻找在TabItem上加载GIF图标的方法,但未找到合适方案,于是尝试使用XamlAnimatedGif库。

我选择了该库的最新版本并安装了对应的NuGet包,编写了如下XAML代码:

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestProject"
    xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl Name="TabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged" BorderBrush="LightGray"  Grid.Row="1" >
            <TabControl.Template>
                <ControlTemplate TargetType="TabControl">
                    <Grid KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="true" ClipToBounds="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="ColumnDefinition0"/>
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                            <RowDefinition x:Name="RowDefinition1" Height="*"/>
                        </Grid.RowDefinitions>
                        <WrapPanel x:Name="HeaderPanel" Panel.ZIndex ="1" KeyboardNavigation.TabIndex="1" Grid.Column="0" Grid.Row="0" Margin="2,2,2,0" IsItemsHost="true"/>
                        <Border x:Name="ContentPanel" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" KeyboardNavigation.TabNavigation="Local"
                            KeyboardNavigation.DirectionalNavigation="Contained" KeyboardNavigation.TabIndex="2" Grid.Column="0" Grid.Row="1">
                            <ContentPresenter x:Name="PART_SelectedContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  Margin="{TemplateBinding Padding}" ContentSource="SelectedContent"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </TabControl.Template>

            <TabControl.Resources>
                <DataTemplate x:Key="TabHeader" DataType="TabItem">
                    <DockPanel Width="150">
                        <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0"  Background="Transparent"  BorderBrush="Transparent"
                                    BorderThickness="0.0"  Width="9" Height="9" Padding="0" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
                            <Image Width="9" Height="9"></Image>
                        </Button>

                        <!--<Image  x:Name="BusyIcon" gif:ImageBehavior.AnimatedSource="../icons/Loading_icon.gif" Width="9" Height="9" Visibility="Visible"></Image>-->
                        <TextBlock  Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" />

                    </DockPanel>
                </DataTemplate>

            </TabControl.Resources>
        </TabControl>

    </Grid>
</Window>

运行应用程序时,出现如下异常:

Could not load file or assembly 'XamlAnimatedGif, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

请问该如何避免此异常?


解决方法

按以下步骤排查修复:

  • 确认NuGet包安装状态
    右键项目→管理NuGet程序包,检查XamlAnimatedGif包是否完整安装。若存在异常,先卸载再重新安装最新稳定版本。
  • 设置程序集复制本地
    在项目的「引用」节点下找到XamlAnimatedGif,右键→属性,将「复制本地」设置为True,确保程序运行时能复制该程序集到输出目录。
  • 修正XAML命名空间
    替换原URL格式的命名空间,改用程序集限定的声明:
    xmlns:gif="clr-namespace:XamlAnimatedGif;assembly=XamlAnimatedGif"
    
  • 清理缓存并重建项目
    删除项目的bin和obj目录,重新生成解决方案,避免旧缓存文件导致的加载问题。
  • 排查依赖缺失
    若以上步骤无效,使用fuslogvw.exe(程序集绑定日志查看器)查看详细绑定失败日志,定位具体缺失的依赖项后针对性修复。

内容的提问来源于stack exchange,提问作者JuniorDev13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 23:03:24