[英]FormatException in application iOS Xamarin(C#)
在我的移動應用(iOS)中,此代碼中包含FormatException:
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());
};
}
}
我認為我在這方面的問題
price_defoult = float.Parse( ((string)myjsondetail["price"]).Replace(".00", ".0") );
但我現在不在我的問題所在。 在模擬器中,我的代碼正常工作,但在iPhone 5s中,我有:
FormatException輸入的字符串格式不正確。 (mscorlib)
System_Runtime_CompilerServices_AsyncMethodBuilderCore__ThrowAsyncm__0_object中的SIGABRT崩潰
System_Xml_XmlTextReaderImpl_Read中的SIGABRT崩潰
我解決了我的問題:
刪除第6行float price_defoult;
在行price001.Text = myjsondetail["price"]+" грн";
刪除+" грн"
使用此行float price_defoult = float.Parse(price001.Text, CultureInfo.InvariantCulture)
帶來類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.