What's Wrong with this StringFormat?

NickANickA USMember ✭✭

This isn't preventing anything from compiling or working right. The line does format as currency. But when I hover over where it says the error is, it says "} expected". There's nothing wrong here, just a XAML bug I'm assuming.

And check out the errors. Why do they show as errors, yet the project builds and runs just fine? And the line in question is actually line 79, not 78 like the error reports.

Not a big deal, I just hate seeing any squiggly lines in my code when there's nothing wrong, and errors that aren't errors.

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited March 12

    Let's see the rest of 78
    If its really missing a right brace on 78 like it reports then it will have trouble interpreting the following line.

    VS2017-VS2019 otherwise doesn't raise a complaint for StringFormat in XAML - at least that I've seen it raise a false error.

  • NickANickA USMember ✭✭

    Here's the lines. I have this happen in a few XAML files, and sometimes a clean/rebuild takes them away, but they come back on the next change. Everything still works, it's just bugs me.

    I suspect it might be something else in the XAML, because I've used that same StringFormat on a couple other pages and it's fine.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The format code is technically lower case. Though upper works, its not really the spec.
    Maybe VS cares about that in order to be picky.
    http://blog.stevex.net/string-formatting-in-csharp/

    I also wonder about the '2'. {0:c} should be all you need. The number of decimal places comes from the localization of the device and is normally 2 already.

  • Sahadev_GuptaSahadev_Gupta INMember ✭✭✭
    Accepted Answer

    @NickA try this

    <Label Text="{Binding Amount, StringFormat='{}{0:C2}'}"

  • NickANickA USMember ✭✭

    @Sahadev_Gupta said:
    @NickA try this

    <Label Text="{Binding Amount, StringFormat='{}{0:C2}'}"

    This was it, thanks! My OCD is now satisfied.

    That explains why this works too:

    Text="{Binding Amount, StringFormat='This is a test {0:C2}'}"

    So must be you need some kind of string, even empty, before the format.

  • Sahadev_GuptaSahadev_Gupta INMember ✭✭✭

    @NickA i also dont have any idea about that. When I updated my XF i too got that same issue, so i just added empty string before it.

Sign In or Register to comment.