应用程序中的 FormatException iOS Xamarin(C#)
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 5 秒后我有:
FormatException Input string was not in a correct format. (mscorlib)
SIGABRT Crash in System_Runtime_CompilerServices_AsyncMethodBuilderCore__ThrowAsyncm__0_object
SIGABRT Crash in System_Xml_XmlTextReaderImpl_Read
我的问题解决了:
删除第 6 行 float price_defoult;
行 price001.Text = myjsondetail["price"]+" грн";
删除 +" грн"
使用此行 float price_defoult = float.Parse(price001.Text, CultureInfo.InvariantCulture)
来输入类型。
在我的移动应用程序 (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 5 秒后我有:
FormatException Input string was not in a correct format. (mscorlib)
SIGABRT Crash in System_Runtime_CompilerServices_AsyncMethodBuilderCore__ThrowAsyncm__0_object
SIGABRT Crash in System_Xml_XmlTextReaderImpl_Read
我的问题解决了:
删除第 6 行 float price_defoult;
行 price001.Text = myjsondetail["price"]+" грн";
删除 +" грн"
使用此行 float price_defoult = float.Parse(price001.Text, CultureInfo.InvariantCulture)
来输入类型。