简体   繁体   English

Xamarin应用程序可在模拟器上运行,但不能在设备上运行

[英]Xamarin app works on emulator but not on device

I´ve done a simple app which convert an amount of Bitcoins to a currency. 我已经完成了一个简单的应用程序,可以将一定数量的比特币转换为货币。

The app works fine on my android emulator(Nexus 5x). 该应用程序可以在我的Android模拟器(Nexus 5x)上正常运行。 My problem is that the app throws: 我的问题是该应用抛出:

System.FormatException: Input string was not in a correct format System.FormatException:输入字符串的格式不正确

When I test it on my android device. 当我在Android设备上对其进行测试时。 I´ve debuged both my emulator and device and it´s no diffrence in the parameters. 我已经调试了我的模拟器和设备,并且参数没有差异。

Do someone have any thought why this exception occurs? 有人想到为什么会发生此异常吗?

stringRate参数

public static async Task<decimal> ConvertCurrency(string curr, decimal amount)
    {
        var currency = await GetCurrency(curr);
        var stringRate = currency.Rate;
        var rate = decimal.Parse(stringRate);//this is where the it crash
        var result = rate * amount;
        return result;
    }

That comma delimiter is your problem. 该逗号分隔符是您的问题。 You would need to change your parse Method to detect the Current culture and parse using that IFormatProvider 您将需要更改解析方法以检测当前区域性并使用该IFormatProvider解析

Use the overload(s) of decimal.Parse : 使用decimal.Parse

string stringRate = "118,130.4542";
decimal d = decimal.Parse(stringRate, 
                          NumberStyles.Currency, 
                          new CultureInfo("en-Us").NumberFormat);
//Output: 118130.4542M

d = decimal.Parse(stringRate, new CultureInfo("en-Us").NumberFormat);
//Output: 118130.4542M

Most likely that happens because of the language settings on your phone. 这很可能是由于手机上的语言设置引起的。 If you try to parse a string from the web using default decimal.Pars function, it would try to identify the separator based on your language settings (or local, it depends). 如果尝试使用默认的decimal.Pars函数从Web解析字符串,它将尝试根据您的语言设置(或本地,取决于它)来识别分隔符。 That mean that if a phone use a "," as a decimal separator, it would try to look for it into input string even if the string come in a format 12.32. 这意味着,如果电话使用“,”作为小数点分隔符,即使该字符串的格式为12.32,它也会尝试将其查找到输入字符串中。 To fix that, pass a culture or implicit separator into parser. 要解决此问题,请将区域性或隐式分隔符传递到解析器中。

decimal.TryParse(EnglishDecimal, out b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Android应用程序可在模拟器上运行,使用WebRequest时在真实设备上崩溃 - Xamarin Android app works on emulator, crashes on real device when using WebRequest Xamarin.Forms应用程序在设备上的MigrateAsync而非模拟器上崩溃 - Xamarin.Forms app crashes on MigrateAsync on the device but not on the emulator 当前的Xamarin应用不在模拟器上吗? - Current Xamarin app not on emulator? 无法在 xamarin 中创建表 sqlite 表(仅限设备)模拟器工作正常 - Unable to create table sqlite table in xamarin(Device only) Emulator works fine Xamarin 表单,在 Visual Studio Android 模拟器上运行的应用程序但在真实设备上崩溃 - Xamarin forms, App running on Visual Studio Android Emulator But crashes on real device xamarin android apk安装在模拟器上,但未安装在设备上 - xamarin android apk installs on emulator but not on device 使用Xamarin的Android模拟器无法打印设备日志 - Device Logs not printing for android emulator using Xamarin Windows Phone乘以浮点数可在模拟器上运行,但不能在设备上运行 - Windows Phone multiplying floats works on emulator but not on device 绘制深度适用于WP7仿真器,但不适用于设备 - Draw depth works on WP7 emulator but not device 应用程序的快捷方式不会在 Xamarin 的模拟器中退出 - Shortcut of app doesn't exit in emulator in Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM