Page 1 of 1
Static Members / Singleton class
Posted: 28 Nov 2021, 21:15
by simonb
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?
Re: Static Members / Singleton class
Posted: 30 Nov 2021, 12:12
by sfernandez
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?
Re: Static Members / Singleton class
Posted: 01 Dec 2021, 19:35
by simonb
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?
Re: Static Members / Singleton class
Posted: 01 Dec 2021, 22:28
by simonb
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 :)
Re: Static Members / Singleton class
Posted: 02 Dec 2021, 10:24
by sfernandez
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
...
}