我正在寻找一种方法来在我的选项卡项目上加载 gif 图标,但我没有得到我需要的东西。我试过这个库:XamlAnimatedGif.
事实上,我选择了最新版本并安装了它的Nuget包。我写了这段代码:
<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>
当我运行我的应用程序时,我得到这个异常:
无法加载文件或程序集“XamlAnimatedGif,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件。
如何防止这种异常?
你的 XAML 是错误的,因为你混合了两个不同的项目。
你用
https://github.com/XamlAnimatedGif/XamlAnimatedGif
新版本的XML命名空间,但是旧版本的附加属性AnimatedSource
。程序集很可能找不到,因为你安装了旧版本的包。
安装WpfAnimatedGif包。
添加这个 XML 命名空间:
`xmlns:gif="http://wpfanimatedgif.codeplex.com"
使用
AnimatedSource
的ImageBehavior
属性指定GIF图像。
<Image x:Name="BusyIcon" gif:ImageBehavior.AnimatedSource="../icons/Loading_icon.gif"/>
安装 XamlAnimatedGif 包。
添加这个 XML 命名空间:
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
使用
SourceUri
的AnimationBehavior
属性指定GIF图像。
<Image x:Name="BusyIcon" gif:AnimationBehavior.SourceUri="../icons/Loading_icon.gif"/>
我收到这个错误是因为我安装了两个版本,WpfAnimatedGif 和 XamlAnimatedGif;我删除了旧版本,重新启动 Visual Studio,错误消失了。
@JuniorDev13 我通过添加解决了这个问题
AnimationBehavior.SetRepeatBehavior(imgCircle, System.Windows.Media.Animation.RepeatBehavior.Forever)
在 .cs 文件中
我认为问题是没有指定
using XamlAnimatedGif;
命名空间到 .cs 文件