问题概述我需要获取购买产品价格的数值,为此,我需要解析从我收到的FormattedPrice对象as documented here收到的ProductListing。这里的问题是,我需要考虑所有边缘情况,例如用户位于外国的情况,即该系统在该语言环境下运行,但设置了商店以从其本地接收内容。例如,我目前将我的电脑(GB区域,GB系统语言)设置为从意大利商店接收内容,因此我收到的价格是欧元,并且用逗号作为小数点分隔符,但是当我尝试使用或CultureInfo.CurrentCulture始终返回GB,因此它将始终无法解析,因为它将使用GB NumberFormat设置解释意大利价格。由于我需要初始化与商店区域相关的CultureInfo对象,因此是否可以通过代码检索此信息?谢谢。我已经尝试过的事情:Using the currency code provided by StoreListing/ProductListing使用RegionInfo.CurrentRegion,无论如何返回GB使用CultureInfo.CurrentCulture,无论如何返回GB使用CultureInfo.CurrentUICulture,无论如何返回GBLooking at the UWP Globalization documentationLooking at the UWP Store documentation 最佳答案 这是CultureInfo类的问题。您应该使用GetLocaleInfoEx的Win32 API来获取CultureInfo。您可以从C ++执行此操作,然后从C#调用它,也可以使用PInvoke从C#执行此操作,因为它已经存在于dll文件中。无需从头开始重新执行此操作,因为this网站已经为CultureInfoHelper API制作了GetLocaleInfoEx包装器。您可以这样使用它:CultureInfo cultureInfo = CultureInfoHelper.GetCurrentCulture();CultureInfoHelper脚本:public class CultureInfoHelper{ [DllImport("api-ms-win-core-localization-l1-2-0.dll", CharSet = CharSet.Unicode)] private static extern int GetLocaleInfoEx(string lpLocaleName, uint LCType, StringBuilder lpLCData, int cchData); private const uint LOCALE_SNAME = 0x0000005c; private const string LOCALE_NAME_USER_DEFAULT = null; private const string LOCALE_NAME_SYSTEM_DEFAULT = "!x-sys-default-locale"; private const int BUFFER_SIZE = 530; public static CultureInfo GetCurrentCulture() { var name = InvokeGetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME); if (name == null) { name = InvokeGetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME); if (name == null) { // If system default doesn't work, use invariant return CultureInfo.InvariantCulture; } } return new CultureInfo(name); } private static string InvokeGetLocaleInfoEx(string lpLocaleName, uint LCType) { var buffer = new StringBuilder(BUFFER_SIZE); var resultCode = GetLocaleInfoEx(lpLocaleName, LCType, buffer, BUFFER_SIZE); if (resultCode > 0) { return buffer.ToString(); } return null; }}关于c# - 使用自定义商店区域解析货币,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43936285/
10-13 03:24