gvol1
Topic Author
Posts: 3
Joined: 29 Aug 2022, 22:11

Bind the source image property from the view model to a key in the resource dictionary

30 Oct 2023, 21:12

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:
<Image Source="{Binding ImageResourceKey, Converter={StaticResource BindingToStaticResourceConverter}}"></Image>
This is the structure of the Atlas:
<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>
I tried using this converter, but I can't find the resource:
 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();
        }
       
    }    

I want to know how to bind static resource keys in the Atlas from the view model.

How should I handle this approach?
 
User avatar
sfernandez
Site Admin
Posts: 2908
Joined: 22 Dec 2011, 19:20

Re: Bind the source image property from the view model to a key in the resource dictionary

31 Oct 2023, 13:13

Are the atlas resources part of the Application Resources?

In that case your converter should look for the resource in that dictionary:
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();
  }
}
Let me know if this works for you.
 
gvol1
Topic Author
Posts: 3
Joined: 29 Aug 2022, 22:11

Re: Bind the source image property from the view model to a key in the resource dictionary

31 Oct 2023, 18:53

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?
 
User avatar
jsantos
Site Admin
Posts: 3811
Joined: 20 Jan 2012, 17:18
Contact:

Re: Bind the source image property from the view model to a key in the resource dictionary

01 Nov 2023, 10:36

1. Is it possible to unload resources from the application when they are not in use?
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.
2. My Unity application contains a really large number of images. Is having some atlases in the application resources the better approach?
Yes, in general. Atlases reduce the number of drawcalls and memory fragmentation.
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?
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.

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest