Best practices for handling resources (.resw) that need to be formatted

Brij 0 Reputation points
2024-03-05T07:39:10.5733333+00:00

I have resources like Formatted by {0} on x

Approach #1

In the backend (.cs file), we create a parameter like so:

string FormattedString = string.format(getResource("abc"), userId);

and use the FormmatedString directly in the xaml file.

Approach #2

Use a custom StringFormatConverter, that takes Resource name as ConverterParameter and in the converter backend get the resource and format it and apply directly in the xaml.

Approach #1, if the original string changes it's value, we have to raise a NotifyPropertyChanged call, but in approach #2 that is eliminated plus the number of parameters also reduces.

Please advice on whether the #2 approach is feasible or is it better to stick with the first approach? and if by using the #2 approach does it make the Front end heavy?

If #2 approach is feasible, please advise on how we can in cooperate multiple bindings if the resource string is like Formatted by {0} on {1} where two places need to be filled.

p.s. I'm using Winui 3

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
745 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,151 Reputation points Microsoft Vendor
    2024-03-06T01:52:43.9166667+00:00

    Hello @Brij,

    As Binding.ConverterParameter said,

    Passing parameters to a converter is a relatively advanced and uncommon scenarios. Most converters used for data binding simply type-convert a source object to a desired target type based solely on the characteristics of the source object, and knowing what the intended destination type is for a particular binding scenario.

    It's also hard to apply two or more strings to be formatted in approach #2 and not recommended.

    0 comments No comments