Following good programming practice, I try to define my "magic strings" as constants. If I need to use the same string in both XAML and C#, I can define it in C# as a constant, but there is no way to reference it in the XAML so I end up having to define it again in as a static resource. In WPF this could be partially solved by defining it as a static readonly string and referencing it with {x:Static}, but this is not available in UWP and {StaticResource} does not have the same functionality.
As an example, I'm trying to do this:
<TextBlock Text="{x:Bind SomeProperty, Converter={StaticResource MyConverter}, ConverterParameter=MyCustomFormat}" />
where MyCustomFormat is a string constant defined and used in the code-behind.
Please add the ability to refer to C# constants and static fields in UWP XAML.
@MikeHillberg do you see this as a WPF to UWP feature gap?
@jeremiahmercier-3136, are you saying that WPF's x:Static and Xaml's x:Bind both support static, but neither support const?
(I agree with having the feature)
@StephenLPeters I've never used WPF. From what I've read, x:Static (WPF) does a little more than x:Bind (UWP). @MikeHillberg If I remember correctly, I believe both of them can be used to refer to static members declared in code. I believe x:Static can refer to enums, but x:Bind cannot. For the purposes of this proposal, I was just looking at constants. I don't know if x:Static allows you to refer to constants declared in code. x:Bind definitely does not. I ran into this when I tried to define a custom format to pass to my converter that I also wanted to use in the code behind. I ended up having to define it twice: once for the XAML and once for the codehind.
I agree that x:Bind should be able to reference enums and const.
Changing the Title of the proposal for something more accurate. Chime if it did wrong.
Most helpful comment
I agree that x:Bind should be able to reference enums and const.