【发布时间】:2015-03-16 10:46:00
【问题描述】:
什么有效
我正在使用一个名为“WPF 动画 GIF”的插件,它允许我将动画 gif 设置为图像的图像源。我的问题是我正在使用来自另一个项目的图像。这些图像是 ResourceDictionary 中的键,如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
<ImageSource x:Key="ImgSyncFailed">Images/Sync Failed.png</ImageSource>
<ImageSource x:Key="ImgSyncSucceeded">Images/Sync Succeeded.png</ImageSource>
<ImageSource x:Key="ImgSyncing">Images/Syncing-Small.gif</ImageSource>
</ResourceDictionary>
资源字典通过引用和XAML添加到另一个项目中:
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/ResourcesDictionary;component/Dictionary.xaml"/>
</Window.Resources>
当您想通过 XAML 添加图像时,这很好用:
<Button x:Name="SyncButton" Focusable="False" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="100" Width="100" Click="SyncButton_OnClick">
<Image x:Name="SyncImage" gif:ImageBehavior.AnimatedSource="{StaticResource ImgSyncSucceeded}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100"/>
</Button>
什么不起作用
当我切换按钮的图像时,我是通过代码来实现的:
private void ChangeSyncImage()
{
switch (syncStatus)
{
case SyncStatus.Synced:
...
case SyncStatus.Desynced:
...
case SyncStatus.Syncing:
var img3 = new BitmapImage();
img3.BeginInit();
img3.UriSource = new Uri("pack://application:,,,/SmartFridgeApplication;component/Images/Syncing-Small.gif");
img3.EndInit();
ImageBehavior.SetAnimatedSource(SyncImage, img3);
break;
}
}
我不喜欢这样设置,它现在不起作用,因为该路径设置为同一个项目中的“图像”。
那么如何设置它以使用 XAML 代码中的 StaticResources?
【问题讨论】:
-
使用 WPF 这真的不应该通过后面的代码来完成,您应该绑定到同步状态,然后使用多重绑定或msdn.microsoft.com/en-us/library/… 或值转换器wpftutorial.net/ValueConverters.html 来选择正确的图像
-
DataTemplateSelector 也可以工作