Reputation: 292
I'm using a plugin called "WPF Animated GIF" which allows me to set a animated gif as an image source to an image. My problem is that I'm using images from another project. These images are keys in a ResourceDictionary, like so:
<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>
The resource dictionary is added to another project through a reference and XAML:
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/ResourcesDictionary;component/Dictionary.xaml"/>
</Window.Resources>
This works just fine when you want to add the image through 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>
When I switch the image of the button, I do so through code:
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;
}
}
I don't like setting it like this, and it doesn't work now, because that path is set to 'Images' which was in the same project.
So how do I set it to use StaticResources like in the XAML code?
Upvotes: 1
Views: 846
Reputation: 5500
using a Value converter you could do this (please note this is untested code provided only as an example of the general principle)
public class SyncConverter : IValueConverter
{
public object Synced {get;set;}
public object Desynced{get;set;}
public object Syncing{get;set;}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
switch (value)
{
case SyncStatus.Synced:
return Synced;
case SyncStatus.Desynced:
return Desynced;
case SyncStatus.Syncing:
return Syncing;
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
with the following in XAML
<Window.Resources>
<l:SyncConverter x:Key="converter" Synced="{StaticResource ImgSyncSucceeded}" Desynced="{StaticResource ImgSyncFailed}" Syncing="{StaticResource ImgSyncing}"/>
</Window.Resources>
<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="{Binding syncStatus, Converter={Static converter}}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100"/>
</Button>
Upvotes: 1
Reputation: 5500
Resource Dictionaries use a look up tree, so first it will look on the controls resources, then it will look up on visual tree until it finds the resource, so as long as your entry in the visual tree is higher than the default value then your value will override
however as you are using the code behind to bypass the static resource then you are breaking the connection if you use TryFindResource(ResourceKey)
you will keep the resource dictionary lookup intact
so
ImageBehavior.SetAnimatedSource(SyncImage, TryFindResource("ImgSyncSucceeded"));
also static resources are supposed to be static ie not changing so using a Dynamic resource could help to https://msdn.microsoft.com/en-us/library/ms748942%28v=vs.110%29.aspx
Upvotes: 2