应用程序iOS Xamarin(C#)中的FormatException
在我的移动应用程序(iOS)中,我有以下代码中的FormatException:应用程序iOS Xamarin(C#)中的FormatException,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,在我的移动应用程序(iOS)中,我有以下代码中的FormatException: public partial class DetailTovarProsmotr : UIViewController { public JsonValue myjsondetail; int count_product; float price_defoult; public string Code1; public DetailTovarProsmotr (I
public partial class DetailTovarProsmotr : UIViewController
{
public JsonValue myjsondetail;
int count_product;
float price_defoult;
public string Code1;
public DetailTovarProsmotr (IntPtr handle) : base (handle)
{
}
public async Task<UIImage> LoadImage (string imageUrl)
{
var httpClient = new HttpClient();
Task<byte[]> contentsTask = httpClient.GetByteArrayAsync (imageUrl);
// await! control returns to the caller and the task continues to run on another thread
var contents = await contentsTask;
// load from bytes
return UIImage.LoadFromData (NSData.FromArray (contents));
}
public async override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Console.Out.WriteLine (detailtitle+" ViewDidLoad metod is run" + myjsondetail["ID"].ToString());
Code1 = myjsondetail ["sku"];
titleproduct001.Text = myjsondetail["post_title"];
postSingleProduct.Text = myjsondetail["post_excerpt"];
Weight001.Text = myjsondetail["weight"]+" г";
price001.Text = myjsondetail["price"]+" грн";
postImage.Image =await this.LoadImage (myjsondetail["img_url"]);
count_product = 1;
countproduct.Text = "1";
//float price_defoult = float.Parse(price001.Text, CultureInfo.InvariantCulture) ;
price_defoult = float.Parse( ((string)myjsondetail["price"]).Replace(".00", ".0") );
plus.TouchUpInside += (o,s) => {
//Console.Out.WriteLine("Нажали плюс!!!!");
countproduct.Text = string.Format("{0}", ++count_product);
price001.Text = string.Format("{0}", count_product*price_defoult + "грн");
};
mines.TouchUpInside += (o,s) => {
// Console.Out.WriteLine("Нажали минусссс!!!!");
countproduct.Text = string.Format("{0}", count_product > 1 ? --count_product : 1);
price001.Text = string.Format("{0}", count_product * price_defoult + "грн");
};
addToCart.TouchUpInside += (o,s) => {
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine (documents, "myFile.xml");
Console.Out.WriteLine("Добавить в корзину!!!!!");
Console.Out.WriteLine(countproduct.Text);
//MessageBarStyleSheet styles = new MessageBarStyleSheet ();
//UIColor colorb = UIColor.Black;
//styles.BackgroundColorForMessageType(MessageType.Info).GetWhite;
// style.BackgroundColorForMessageType = UIColor.Black;
MessageBarManager.SharedInstance.ShowMessage ("Товар добавлен ", "в корзину", MessageType.Info);
//Posrednic singleprod = new Posrednic(myjsondetail);
CartProduct cart = new CartProduct();
int productQty = int.Parse(countproduct.Text);
for (int i = 0; i< productQty; i++) {
cart.Add(myjsondetail.ToString());
}
CartProduct.PrintProducts(cart);
//singleprod.myjsondetail =myjsondetail;
XDocument doc = XDocument.Load(filePath);
var product = new XElement("Product", new XAttribute("Code", Code1), new XAttribute("Qty", countproduct.Text));
var products = doc.Descendants("Products").First(); // Get the first Products node. Throw an exception if not found.
products.Add(product);
File.WriteAllText(filePath, doc.ToString());
doc.Save (filePath);
Console.WriteLine("Smotri tut");
Console.WriteLine (doc.ToString());
};
}
}
但我现在不知道我的问题在哪里。在模拟器中,我的代码正常工作,但在iphone 5s中,我有:
FormatException输入字符串的格式不正确。(mscorlib)
系统\u运行时\u编译器服务\u AsyncMethodBuilderCore\u ThrowAsyncm\u 0\u对象中的SIGABRT崩溃
系统中的SIGABRT崩溃\u Xml\u XmlTextReaderImpl\u读取
我的问题解决了:
删除第6行<代码>浮动价格\u结果代码>
第行price001.Text=myjsondeail[“price”]+“Γцц”代码>删除+“ГППц”
使用这一行float price\u defoult=float.Parse(price001.Text,CultureInfo.InvariantCulture)
作为输入类型。可能模拟器使用的是您设备以外的另一个区域性,并且当模拟器与替换模式匹配时。.00“设备带有类似于“123,00”的值(注意逗号)无法替换和分析。只是一个猜测…你能包括myjsondeail[“price”]的实际值吗?从模拟器和实际设备中替换(“.00”,“.0”)?此代码为“Console.WriteLine(((字符串)myjsondeail[“price”])。替换(“.00”,“.0”);”print 122.0@DylanSI在“price_defoult=float.Parse”((字符串)myjsondeail[“price”])上修改了我的代码。替换(“,”,“)。替换(“.00”,“.0”);”但是结果是格式化的,@Waescher在前面指出,如果您的设备使用与模拟器不同的语言环境,它可能不接受“.”作为浮点数中的有效十进制分隔符。你真的查过了吗?
price_defoult = float.Parse( ((string)myjsondetail["price"]).Replace(".00", ".0") );