【问题标题】:The member "CurrentCulture" is not recognized or is not accessible成员“CurrentCulture”未被识别或无法访问
【发布时间】:2015-02-13 04:02:38
【问题描述】:

我有一个具有以下命名空间的窗口

xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"

包含一个文本框

<TextBox Text="{Binding Path=Price, Mode=TwoWay, StringFormat='C',
                 ConverterCulture={x:Static sysglb:CultureInfo.CurrentCulture}}"
                MaxLines="1" TextAlignment="Right"/>

根据Gusdor's 回复StringFormat Localization issues in wpf,它工作正常,但现在 Visual Studio (2013) 给了我一个“无效标记”-无法识别成员“CurrentCulture”或无法访问错误。

Intellisense 识别并提示 sysglb:CultureInfo.CurrentCulture 但是一旦我离开文本框,我就会收到错误。

能否有好心人告诉我为什么会发生这种情况以及我该怎么做才能解决它? 此外,XAML 编辑器如何设法识别 sysglb:CultureInfo.CurrentCulture 而标记却没有?

干杯 杰夫

【问题讨论】:

  • 你知道吗。我刚刚升级到 VS 2015 并开始收到此错误。真讽刺!你修好了吗?
  • @Gusdor 这不是讽刺
  • 将项目目标框架更改为 .NET Framework 4.6 或更高版本可以解决 VS2013 和 VS2015 的问题。
  • 据我所知,这是 VS2015 中的一个错误。我以与您相同的方式使用 ConverterCulture,并以 .NET 4.5 为目标。设计器崩溃了,但项目仍在构建和运行。作为一种解决方法,暂时移除 ConverterCulture 以让设计器重新工作。

标签: xaml localization


【解决方案1】:

不记得我是从哪里得到的,但它可以工作

using System.Globalization;
using System.Windows.Data;

namespace SomeNamespace
{
    /// <summary>
    /// This class is a fudge because
    /// 
    ///         xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"
    ///         
    ///         <TextBox Grid.Row="2" Grid.Column="1" 
    ///              Text="{Binding Path=SelectedSupporterCategory.Price, Mode=TwoWay, StringFormat='C',
    ///              ConverterCulture={x:Static sysglb:CultureInfo.CurrentCulture}}" 
    ///              UseLayoutRounding="True" MaxWidth="100" HorizontalAlignment="Left" MinWidth="100" HorizontalContentAlignment="Right"/>
    /// 
    ///     is giving 
    ///             Error 29    "The member "CurrentCulture" is not recognized or is not accessible."
    /// 
    /// Instead we use
    /// 
    ///         <TextBox Grid.Row="2" Grid.Column="1" 
    ///              Text="{CultureAwareBinding Path=SelectedSupporterCategory.Price, Mode=TwoWay, StringFormat='C',}" 
    ///              UseLayoutRounding="True" MaxWidth="100" HorizontalAlignment="Left" MinWidth="100" HorizontalContentAlignment="Right"/>
    /// 
    /// </summary>
    public class CultureAwareBinding : Binding
    {
        public CultureAwareBinding()
        {
            ConverterCulture = CultureInfo.CurrentCulture;
        }
    }
}

【讨论】:

    【解决方案2】:

    将项目目标框架更改为 .NET Framework 4.6 或更高版本即可解决问题。

    转到解决方案资源管理器并右键单击受影响的项目 -> 属性 -> 应用程序 -> 目标框架

    【讨论】:

    • 谢谢,但现阶段没有选择
    【解决方案3】:

    在本主题中找到similar suggestionWPF StringFormat={0:C} showing as dollars

    当我启动应用程序并以正确的文化格式显示值时,我的应用程序正在运行,但设计器找不到 CultureInfo.CurrentUICulture 并崩溃

    我在帮助类中使用了静态属性

    public static class WpfHelpers
    {
        public static CultureInfo CurrentCulture { get; set; }
    }
    

    并在绑定中使用它:ConverterCulture={x:Static helpers:WpfHelpers.CurrentCulture}

    我在应用程序启动时设置了该属性

    WpfHelpers.CurrentCulture =
    Thread.CurrentThread.CurrentCulture =
    Thread.CurrentThread.CurrentUICulture = new CultureInfo ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多