Bind the source image property from the view model to a key in the resource dictionary
Hello!
I want to bind the source image property from the view model to a key in the resource dictionary in the Atlas.
Something like this:
This is the structure of the Atlas:
I tried using this converter, but I can't find the resource:
I want to know how to bind static resource keys in the Atlas from the view model.
How should I handle this approach?
I want to bind the source image property from the view model to a key in the resource dictionary in the Atlas.
Something like this:
Code: Select all
<Image Source="{Binding ImageResourceKey, Converter={StaticResource BindingToStaticResourceConverter}}"></Image>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="LogosAtlasDictionary" UriSource="LogosAtlasDictionary.png"/>
<CroppedBitmap x:Key="LogoOne" Source="{StaticResource LogosAtlasDictionary}" SourceRect="1,1,2889,409"/>
<CroppedBitmap x:Key="LogoTwo" Source="{StaticResource LogosAtlasDictionary}" SourceRect="2892,1,1078,152"/>
</ResourceDictionary>
Code: Select all
class BindingToStaticResourceConverter : IValueConverter
{
static FrameworkElement frameworkElement = new FrameworkElement();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return frameworkElement.FindResource(value as string);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
How should I handle this approach?
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: Bind the source image property from the view model to a key in the resource dictionary
Are the atlas resources part of the Application Resources?
In that case your converter should look for the resource in that dictionary:
Let me know if this works for you.
In that case your converter should look for the resource in that dictionary:
Code: Select all
class BindingToStaticResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ResourceDictionary app = Noesis.GUI.GetApplicationResources();
return app[value];
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Re: Bind the source image property from the view model to a key in the resource dictionary
Thanks, does this work for me, I have some questions.
1. Is it possible to unload resources from the application when they are not in use?
2. My Unity application contains a really large number of images. Is having some atlases in the application resources the better approach?
3. If all the images in my application are in atlases in the application resources, will we experience a negative impact when the application is opened?
1. Is it possible to unload resources from the application when they are not in use?
2. My Unity application contains a really large number of images. Is having some atlases in the application resources the better approach?
3. If all the images in my application are in atlases in the application resources, will we experience a negative impact when the application is opened?
Re: Bind the source image property from the view model to a key in the resource dictionary
This is Unity responsibility. When our XAMLs are unloaded we release references to all dependencies. Addressables give you more control on resources lifetime by the way.1. Is it possible to unload resources from the application when they are not in use?
Yes, in general. Atlases reduce the number of drawcalls and memory fragmentation.2. My Unity application contains a really large number of images. Is having some atlases in the application resources the better approach?
Well, it depends on the number of atlases. But I would say, this is a recommended approach if you want to have a set of global textures to be used by your UI.3. If all the images in my application are in atlases in the application resources, will we experience a negative impact when the application is opened?
Who is online
Users browsing this forum: Ahrefs [Bot] and 1 guest