如何解决从 ModalAsync 中更新 ObservableCollection
我有一个需要从推送的 modalasync 更新的 observable 集合。可观察集合基本上是一个购物车。在主页 (MyHome.xaml.cs) 中,我有另一个 observable 集合,当单击项目时,会将模态异步推送到 selctionPage(SelectionPage.xaml.cs)。在此选择页面中,我将一个项目添加到位于 MyHome.xaml 的 Cart 可观察集合中。
我的问题是当我们回去查看购物车时,集合没有更新。我曾尝试在 pushModalAsync 期间发送 MyHomePage 和实际的 Cart ,但这些都不起作用。我还尝试从模态异步中调用 MyHome 函数,但也不起作用。
MyHome.xaml.cs
using System;
using System.Collections;
using System.Collections.Generic;
using HW3.viewmodel;
using HW3.Model;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace HW3.Views
{
public partial class MyHome : ContentPage
{
public ObservableCollection<Product> Cart { get; set; }
Product jello = new ProductByQuantity(1.00,3,"Jello","Canned & Packaged Foods",0001);
public MyHome()
{
InitializeComponent();
BindingContext = new Productviewmodel();
//Cart = new ObservableCollection<Product>();
//Cart.Add(jello);
//Cart.Add(jello);
//add(jello);
}
private async void OnItemSelected(Object sender,ItemTappedEventArgs e)
{
//add(e.Item as Product);
if (e.Item is ProductByWeight)
{
var extra = e.Item as ProductByWeight;
var details = e.Item as Product;
await Navigation.PushModalAsync(new SelectionPage(details.Name,details.Price,details.Description,details.ID,extra.getounces(),extra,Cart,this));
}
else if(e.Item is ProductByQuantity)
{
var extra = e.Item as ProductByQuantity;
var details = e.Item as Product;
//Navigation.PushModalAsync(new NavigationPage(new SelectionPage(details.Name,extra.getUnits(),this)));
await Navigation.PushModalAsync(new SelectionPage(details.Name,this));
}
}
public void add(Product p)
{
}
};
}
SelectionPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using HW3.Model;
using HW3.viewmodel;
using Xamarin.Forms;
namespace HW3.Views
{
public partial class SelectionPage : ContentPage
{
MyCartviewmodel myCartviewmodel = new MyCartviewmodel();
ObservableCollection<Product> pr;
MyHome Home;
Product chicken = new ProductByWeight(12.50,75,"Chicken Breast","Produce",0016);
public ObservableCollection<Product> Cart1 { get; set; }
public SelectionPage(string Name,double Price,string Description,int ID,double units_ounces,Product product,ObservableCollection<Product> p,MyHome home)
{
InitializeComponent();
//property equal cart here
testName.Text = product.Name;
testPrice.Text = "Price: $" + Price.ToString();
testInventory.Text = "Inventory: " + units_ounces.ToString();
testDescription.Text = Description;
slider.Minimum = 1;
slider.Maximum = units_ounces;
sliderNumber.Text = "Value: 1";
pr = p;
Home = home;
}
private void slider_changed(object sender,ValueChangedEventArgs e)
{
var newStep = Math.Round(e.NewValue / 1.0);
slider.Value = newStep * 1.0;
sliderNumber.Text = "Value: " + slider.Value;
}
private async void addButton_clicked(Object sender,System.EventArgs e)
{
string str = sliderNumber.Text;
var result = str.Substring(str.LastIndexOf(" ") + 1);
var chicken = new ProductByWeight(12.50,0016);
//Home.Cart.Add(chicken);
//Home.add(chicken);
//pr.Add(chicken);
Cart1 = new ObservableCollection<Product>();
Cart1.Add(chicken);
Home.Cart = new ObservableCollection<Product>();
Home.Cart = Cart1;
foreach(Product x in Home.Cart)
{
Console.WriteLine("Hello " + x.Name);
}
}
private async void backButton_clicked(Object sender,System.EventArgs e)
{
var chicken = new ProductByWeight(12.50,0016);
//Home.add(chicken);
//Home.Cart.Add(chicken);
//pr.Add(chicken);
Navigation.PopModalAsync();
}
}
}
MyCart.xaml.cs
using System;
using System.Collections.Generic;
using HW3.Model;
using HW3.viewmodel;
using Xamarin.Forms;
namespace HW3.Views
{
public partial class MyCart : ContentPage
{
public List<Product> myCart1 = new List<Product>();
public MyCart()
{
InitializeComponent();
this.BindingContext = new MyHome();
}
}
}
在此先感谢所有提供帮助的人!我已经坚持了一段时间。
解决方法
在 SelectionPage
中,您传入对 Cart ObservableCollection
的引用,但您从未对它进行任何操作。您应该能够添加项目,它们将反映在 MyHome
中,因为它们引用相同的对象。在您的代码中还有几个地方可以创建新对象 - 任何时候您都会获得一个类的新副本,该副本与代码中其他地方使用的副本没有任何联系,并且不会更新该新副本反映在其他页面
更好的方法是将 Cart
创建为 App
类的属性,以便可以从应用的每个页面轻松访问它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。