【问题标题】:Add currency symbols to `TextBlock`将货币符号添加到“TextBlock”
【发布时间】:2013-07-11 10:12:05
【问题描述】:

我正在开发一个 Windows Phone 8 应用程序,我只需要在 XAML 或通过编程方式在 textBlock 中显示货币符号。要获得这样的我必须使用Unicode Characters。 但是在使用这个语句时

    <TextBlock Text="&#xf01;"/> //I Got some characters but using
    <TextBlock Text="&#xf001;"/> //I got a square box
    //Note: &#xf001; or &#xf01; is not a currency symbol

所以我的问题是如何使用 XAML 或 C# 添加 $ YenIndian Rupee 等货币符号,到目前为止我尝试了很多但没有成功。任何答案将不胜感激。

谢谢

【问题讨论】:

    标签: c# windows-phone-7 xaml windows-phone-8


    【解决方案1】:

    实际上我使用了错误的货币代码格式。
    这是Currency Code的列表。

    使用货币符号的正确格式是

       <TextBlock Text="&#x20b9;"/> //for Indian Currency
    

    仍然感谢您提供宝贵的答案,但刚才我发现了这个并且它有效。

    【讨论】:

      【解决方案2】:

      您可以在绑定上使用StringFormat 以将值显示为货币

      例子:

      <TextBlock Text="{Binding Value, StringFormat=C}" />
      

      您可以指定ConverterCulture 以显示$ Yen,Indian Rupee

      <TextBlock Text="{Binding Value, StringFormat=C, ConverterCulture=de-DE}" />
      

      【讨论】:

      • 这对所有人都有可能吗?就像我想创建一个必须显示其符号的货币列表一样。如果我在这里手动定义文化,那么我认为它不会适用于所有人。如果我错了,请纠正我。
      【解决方案3】:

      尝试使用图像或 XAML 路径按钮样式。

      查看Syncfusion's Metro Studio

      最终结果可能是您根据货币调用的图像或您可以动态更改它的样式。

      将此添加到 MainPage.xaml:

      <phone:PhoneApplicationPage.Resources>
      
      
      
          <Style x:Key="PathBasedAppBarButtonStyle" BasedOn="{StaticResource AppBarButtonStyle}" TargetType="ButtonBase">
              <Setter Property="ContentTemplate">
                  <Setter.Value>
                      <DataTemplate>
                          <Path Width="18" Height="18" 
                   Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"
                             Margin="0 0 2 0" RenderTransformOrigin="0.5,0.5" 
                   Fill="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" 
                   Data="{Binding Path=Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                      </DataTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      
      
          <Style x:Key="RupeeAppBarButtonStyle" BasedOn="{StaticResource PathBasedAppBarButtonStyle}" TargetType="ButtonBase">
              <Setter Property="AutomationProperties.Name" Value="Rupee"/>
              <Setter Property="AutomationProperties.AutomationId" Value="RupeeAppBarButton"/>
              <Setter Property="Content" Value="F1M379.388,262.844L409.946,262.844C410.118,262.867,410.203,262.953,410.203,263.104L410.203,266.01C410.203,266.183,410.118,266.269,409.946,266.269L398.446,266.269 398.446,266.527C400.083,267.411,401.354,269.219,402.258,271.954L402.387,272.891 409.946,272.891C410.118,272.912,410.203,272.998,410.203,273.149L410.203,276.055C410.203,276.229,410.118,276.314,409.946,276.314L402.677,276.314C402.677,279.566 400.686,282.732 396.702,285.811 393.17,287.836 390.662,288.848 389.175,288.848 389.175,288.934 389.09,288.977 388.918,288.977L405.326,310.231 405.326,310.49 399.093,310.49C399.028,310.49,392.685,302.264,380.067,285.811L380.067,285.682 382.037,285.682C389.014,285.682 393.202,283.174 394.602,278.156 394.688,277.553 394.731,277.069 394.731,276.702L394.731,276.314 379.388,276.314C379.216,276.314,379.13,276.229,379.13,276.055L379.13,273.149C379.151,272.977,379.237,272.891,379.388,272.891L394.602,272.891C394.086,270.932 392.987,269.337 391.308,268.11 388.981,266.882 386.861,266.269 384.944,266.269L379.388,266.269C379.216,266.269,379.13,266.183,379.13,266.01L379.13,263.104C379.151,262.931,379.237,262.844,379.388,262.844z"/>
          </Style>
      
      
      
      
      
          <Style x:Key="YenAppBarButtonStyle" BasedOn="{StaticResource PathBasedAppBarButtonStyle}" TargetType="ButtonBase">
              <Setter Property="AutomationProperties.Name" Value="Yen"/>
              <Setter Property="AutomationProperties.AutomationId" Value="YenAppBarButton"/>
              <Setter Property="Content" Value="F1M304.401,-42.8347L304.401,-55.4694 293.272,-55.4694 293.272,-58.9883 304.401,-58.9883 304.401,-64.2292 293.272,-64.2292 293.272,-67.7468 302.678,-67.7468 290.544,-89.5013 297.293,-89.5013 304.759,-74.7116C305.909,-72.2695,306.77,-70.3314,307.631,-68.1777L307.847,-68.1777C308.564,-70.1882,309.498,-72.4134,310.718,-74.8548L318.472,-89.5013 325.078,-89.5013 312.226,-67.7468 321.56,-67.7468 321.56,-64.2292 310.36,-64.2292 310.36,-58.9883 321.56,-58.9883 321.56,-55.4694 310.36,-55.4694 310.36,-42.8347 304.401,-42.8347z"/>
          </Style>
      
      
      
      
          <Style x:Key="DollarAppBarButtonStyle" BasedOn="{StaticResource PathBasedAppBarButtonStyle}" TargetType="ButtonBase">
              <Setter Property="AutomationProperties.Name" Value="Dollar"/>
              <Setter Property="AutomationProperties.AutomationId" Value="DollarAppBarButton"/>
              <Setter Property="Content" Value="F1M183.025,3329.47L183.025,3337.06 177.917,3337.06 177.917,3329.68C173.181,3329.66,168.773,3328.59,164.689,3326.46L164.689,3316.77C166.044,3317.87 168.062,3318.88 170.744,3319.82 173.427,3320.75 175.818,3321.28 177.917,3321.42L177.917,3308.69C172.458,3306.66 168.732,3304.46 166.737,3302.09 164.742,3299.72 163.745,3296.84 163.745,3293.43 163.745,3289.77 165.039,3286.65 167.63,3284.07 170.219,3281.49 173.648,3279.99 177.917,3279.57L177.917,3273.07 183.025,3273.07 183.025,3279.43C187.947,3279.67,191.622,3280.46,194.048,3281.81L194.048,3291.26C190.782,3289.28,187.109,3288.06,183.025,3287.62L183.025,3300.88C188.135,3302.73 191.797,3304.83 194.012,3307.2 196.228,3309.57 197.337,3312.43 197.337,3315.79 197.337,3319.66 196.102,3322.78 193.63,3325.15 191.155,3327.52 187.622,3328.96 183.025,3329.47z M177.917,3298.85L177.917,3287.76C174.673,3288.35 173.054,3290.04 173.054,3292.84 173.054,3295.24 174.673,3297.24 177.917,3298.85z M183.025,3310.68L183.025,3321.28C186.361,3320.77 188.031,3319.1 188.031,3316.28 188.031,3313.99 186.361,3312.13 183.025,3310.68z"/>
          </Style>
      </phone:PhoneApplicationPage.Resources>
      


      并且您可以动态更改样式:

      button.Style = (Style)Application.Current.Resources["DefaultMusicButtonStyle"];
      

      【讨论】:

      • 我喜欢你的建议,谢谢:) 因为它们不是问题的一部分,我不能接受它作为答案,我的问题很清楚使用Unicode Characters。但我真的很喜欢你的建议:)
      【解决方案4】:

      你可以为国家获取CultureInfo,然后使用NumberFormatInfo获取currency symbol

      // Display the culture name and currency symbol.
      NumberFormatInfo nfi = ci.NumberFormat;
      sb.AppendFormat("The currency symbol for '{0}' is '{1}'",
      ci.DisplayName, nfi.CurrencySymbol);
      sb.AppendLine();
      

      【讨论】:

      • 你的意思是我应该从设备中获取文化信息,然后像这样设置它的值?如果是这样,我很抱歉,但我必须为所有货币都这样做,因为我必须列出一个清单。
      • 不,我不建议获取当前的文化信息。您可以通过调用 Cultureinfo 类msdn.microsoft.com/en-us/library/… 的 GetCultures 方法获取所有文化的列表,然后将它们添加到列表中。如果你这样做,你就不需要维护一个静态的货币符号列表
      • 好的,让我试试。很快就会通知您。
      • 对不起,它不适用于 Windows Phone 8 它只支持 Windows 8 和其他
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多