samc
Topic Author
Posts: 79
Joined: 21 Aug 2019, 19:22

Static Members Not Supported ?

02 Jul 2024, 00:02

Hi,

I'm trying to bind a field to a constant in code.

Here is my xaml:
<MyUserControl MyBindingId="{x:Static local:Action.Id}">
And my C# code looks like this:
namespace Foo.Bar
{
  public static class Action
  {
    public const int Id = 42;
  }
}
When I do this, I'm getting an error at runtime:
Static members not supported. Can't use 'Foo.Bar.Action.Interact'
I'm confused about the error - is it saying the 'x:Static' part in my xaml is not allowed? Or is the problem that my class is static? The variable is a constant so I'm not sure if it's a mis-detection on that?

thanks,
sam
 
User avatar
sfernandez
Site Admin
Posts: 3112
Joined: 22 Dec 2011, 19:20

Re: Static Members Not Supported ?

02 Jul 2024, 09:27

Hi Sam,

It means that current x:Static implementation does not support accessing static members in classes (see here), it only works with enum values and a few more scenarios described in the documentation.

Perhaps you can use the DP default value option in this case, could that work for you?
 
samc
Topic Author
Posts: 79
Joined: 21 Aug 2019, 19:22

Re: Static Members Not Supported ?

02 Jul 2024, 23:20

Hmmm - My use case is that I'm trying to use code generated by Rewired (we are using it for input) to bind controls to specific actions in order to create a user interface so users can rebind controls. The code that Rewired generates uses constants rather than an enum value. I'll see if I can do something else on my end.

thanks,
sam
 
User avatar
sfernandez
Site Admin
Posts: 3112
Joined: 22 Dec 2011, 19:20

Re: Static Members Not Supported ?

03 Jul 2024, 08:40

I was referring to this case, I think it would work the same for const values.
public class SizeHelper : DependencyObject
{
  public static float BorderWidth { get { return 16.0f } };

#if NOESIS
  public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register(
    "BorderWidth", typeof(float), typeof(SizeHelper),
    new PropertyMetadata(SizeHelper.BorderWidth));
#endif
}

Who is online

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