simonb
Topic Author
Posts: 14
Joined: 22 May 2021, 16:35

Static Members / Singleton class

28 Nov 2021, 21:15

Hi guys

any update on 1305

I'm trying to see if its possible to use a Singleton class to easily import and deal with a huge amount of localised text in a dictionary
using some thing like:
    Content="{Binding Source={x:Static local:Translate.Instance}, Path=GameTexts[TXT_EXAMPLE_KEY]}
It's a simple approach that works really well in Blend, but getting the 'not supported' message in Unity/Noesis.

do you have an idea, if and when you might get round to looking at it?
 
User avatar
sfernandez
Site Admin
Posts: 2417
Joined: 22 Dec 2011, 19:20

Re: Static Members / Singleton class

30 Nov 2021, 12:12

Hi Simon,

I'm sorry to say that we haven't implemented static members yet for the following release.

But as described in our documentation: https://www.noesisengine.com/docs/Gui.C ... cextension, there are some scenarios where x:Static already works and you can can use to workaround it. If you create a dependency property in your Translate class with the Instance as its default value, that will work in Noesis:
public class Translate
{
  public static Translate Instance { get; } = new Translate();
  
  public static readonly DependencyProperty InstanceProperty = DependencyProperty.Register(
    "Instance", typeof(Translate), typeof(Translate), new PropertyMetadata(Translate.Instance));

  ...
}
Can you try that?
 
simonb
Topic Author
Posts: 14
Joined: 22 May 2021, 16:35

Re: Static Members / Singleton class

01 Dec 2021, 19:35

Hi Sergio,

thanks for the work around. I have added the dependency object as written, but when I try and access it in XAML using my code above
VS gives the following error:
'Translate' type must derive from DependencyObject.

I tried having Translate inherit from Dependency object but it wont let me (Sealed class).
Am I missing some thing obvious?
 
simonb
Topic Author
Posts: 14
Joined: 22 May 2021, 16:35

Re: Static Members / Singleton class

01 Dec 2021, 22:28

Ah never mind, figured it out.

That approach does work in Noesis/Unity but not in Blend were I was trying to test it.
Needed to ifdef it & make "Translate" a straightforward Singleton class in the WPF application.

thanks for the help :)
 
User avatar
sfernandez
Site Admin
Posts: 2417
Joined: 22 Dec 2011, 19:20

Re: Static Members / Singleton class

02 Dec 2021, 10:24

Hi Simon,

It works in Blend when running the project by using the static property, but I didn't notice before that in the designer the binding doesn't work once you add the dependency property with the same name. So as you said, this will require to ifdef the dependency property definition only for Noesis:
public class Translate
{
  public static Translate Instance { get; } = new Translate();
#if NOESIS
  public static readonly DependencyProperty InstanceProperty = DependencyProperty.Register(
    "Instance", typeof(Translate), typeof(Translate), new PropertyMetadata(Translate.Instance));
#endif
  ...
}

Who is online

Users browsing this forum: Google [Bot] and 2 guests