【发布时间】:2014-04-27 23:11:59
【问题描述】:
我目前正在尝试将隔离存储添加到 Windows Phone / C# 上的简单宠物店应用程序。 (请注意,这是我第一次尝试隔离存储,所以我可能会离开)
我只想将隔离存储添加到我的“购物篮”中,这样当您将商品添加到购物篮然后关闭应用程序时,当您重新启动应用程序时,该商品仍会在我的购物篮中。
首先,这里是我在篮子中添加“动物物品”的代码。
private void list_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
Shop selectedPet = list.SelectedItem as Shop;
//thisApp.tempItem = selectedPet;
String photo = selectedPet.Photo;
String breed = selectedPet.Breed;
String name = selectedPet.Name;
DateTime age = selectedPet.Age;
double price = selectedPet.Price;
NavigationService.Navigate(new Uri("/Profile.xaml?&breed=" + breed + "&age=" + age + "&price=" + "€" + price + "&name=" + name + "&" + "photo=" + photo, UriKind.Relative));
}
这段代码将所选项目的个别信息传递到“个人资料页面”
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
try
{
//String temp1 = thisApp.tempItem.Name;
string photo;
string name;
string age;
string breed;
string price;
if (NavigationContext.QueryString.TryGetValue("photo", out photo))
{
imgPhotoHolder.Source = null;
BitmapImage myimage = new BitmapImage
(new Uri("/Assignment 1;component/Images/" + photo, UriKind.RelativeOrAbsolute));
imgPhotoHolder.Source = myimage;
}
if (NavigationContext.QueryString.TryGetValue("name", out name))
{
nameTxtBlock.Text = name;
}
if (NavigationContext.QueryString.TryGetValue("age", out age))
{
ageTxtBlock.Text = age;
}
if (NavigationContext.QueryString.TryGetValue("breed", out breed))
{
breedTxtBlock.Text = breed;
}
if (NavigationContext.QueryString.TryGetValue("price", out price))
{
priceTxtBlock.Text = price;
}
Find(photo);
}
catch
{
MessageBox.Show("Image did not load...");
}
}
然后,这会将信息片段添加到个人资料页面,但出现了问题,我没有传递对象,而是传递了单独的信息,因此基本上分解了对象,所以为了纠正这个问题,我使用了下面的代码并且能够在按钮按下事件时将其传递到我的购物篮中。
private void Find(String str)
{
foreach (Shop pet in thisApp.myshop)
{
if (pet.Photo == str)
{
index = thisApp.myshop.IndexOf(pet);
}
}
}
private void btnAddToBasket_Click(object sender, RoutedEventArgs e)
{
bool found = false;
try
{
foreach (Shop pet in thisApp.basket)
{
if(thisApp.myshop[index].Photo == pet.Photo)
{
found = true;
}
}
if (found)
{
MessageBox.Show("Item already in Basket");
}
else
{
thisApp.basket.Add(thisApp.myshop.ElementAt(index));
}
}
catch
{
MessageBox.Show("That didn't work...");
}
}
现在问题出现了,我已将隔离存储代码放入 App.xaml 中,如此处所示。
public void WriteBasketToStorage()
{
using (IsolatedStorageFile cart = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("Cart.xml",
FileMode.Create, cart))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
{
writer.WriteStartElement("Pets");
foreach (Shop currPet in basket)
{
writer.WriteStartElement("Pet");
writer.WriteElementString("Name", currPet.Name);
writer.WriteElementString("Age", currPet.Age.ToString());
writer.WriteElementString("Breed", currPet.Breed);
//writer.WriteElementString("Type", currPet.Type);
//writer.WriteElementString("Stock", currPet.Stock.ToString());
writer.WriteElementString("Price", currPet.Price.ToString());
writer.WriteElementString("Photo", currPet.Photo);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
}
}
}
public void RetrieveBasketData()
{
try
{
using (IsolatedStorageFile cart = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream isoStream = cart.OpenFile("Basket.xml", FileMode.Open))
{
try
{
XmlReader reader = XmlReader.Create(isoStream);
reader.ReadToDescendant("Pet");
basket.Clear();
while (reader.Read())
{
//reader.MoveToFirstAttribute();
reader.ReadToFollowing("Name");
string name = reader.ReadElementContentAsString();
reader.ReadToFollowing("Age");
string age = reader.ReadElementContentAsString();
reader.ReadToFollowing("Breed");
string breed = reader.ReadElementContentAsString();
//reader.ReadToFollowing("Type");
//string type = reader.ReadElementContentAsString();
//reader.ReadToFollowing("Stock");
//string stock = reader.ReadElementContentAsString();
reader.ReadToFollowing("Price");
string price = reader.ReadElementContentAsString();
reader.ReadToFollowing("Photo");
string photo = reader.ReadElementContentAsString();
DateTime date = DateTime.Parse(age);
decimal price1 = Decimal.Parse(price);
//basket.Add(new Shop(name, age, breed, type, stock, date, price1, photo ));
basket.Add(new Shop(photo, breed, name, date, price1));
}
}
catch
{
}
}
}
catch (IsolatedStorageException ise)
{
MessageBox.Show(ise.Message);
}
}
我已尝试使用此代码在我的购物篮页面中检索它
public void ReadBasketDetailsFromStorage()
{
try
{
using (IsolatedStorageFile petStore = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream isoStream = petStore.OpenFile("Basket.xml", FileMode.Open))
{
try
{
XmlReader reader = XmlReader.Create(isoStream);
reader.ReadToDescendant("Pet");
while (reader.Read())
{
//reader.MoveToFirstAttribute();
reader.ReadToFollowing("Name");
string name = reader.ReadElementContentAsString();
reader.ReadToFollowing("Age");
string age = reader.ReadElementContentAsString();
reader.ReadToFollowing("Breed");
string breed = reader.ReadElementContentAsString();
//reader.ReadToFollowing("Type");
//string type = reader.ReadElementContentAsString();
//reader.ReadToFollowing("Stock");
//string stock = reader.ReadElementContentAsString();
reader.ReadToFollowing("Price");
string price = reader.ReadElementContentAsString();
reader.ReadToFollowing("Photo");
string photo = reader.ReadElementContentAsString();
DateTime date = DateTime.Parse(age);
decimal price1 = Decimal.Parse(price);
//basket.Add(new Shop(name, age, breed, type, stock, date, price1, photo ));
thisApp.basket.Add(new Shop(photo, breed, name, date, price1));
}
}
catch
{
}
}
}
catch (IsolatedStorageException ise)
{
MessageBox.Show(ise.Message);
}
}
我遇到的错误是它不包含需要 5 个参数的构造函数。我试图通过使用不需要的字段(如“类型”和“库存”)来修复它,但它随后说它不包含需要 7 个参数等的构造函数。
目前我不确定如何处理此问题,非常感谢您提供有关此问题的建议/帮助。如果您需要更多代码来完全理解我的问题,请索取,我会将其添加到帖子中。
提前致谢, 杰森
【问题讨论】:
-
@Romasz 的答案应该可以正常工作。但是,如果您要存储结构化数据(您就是这样),数据库是更好的解决方案。你可以在这里阅读它们:msdn.microsoft.com/en-us/library/windowsphone/develop/…
标签: c# windows-phone-8 isolatedstorage