C# 绑定MobileServiceCollection未在UI(MVVM)中更新
我的视图模型中有一个移动服务集合,它从云中的移动服务数据库获取数据。我正在尝试将此MobileServiceCollection绑定到ui中的列表框(带有项目模板)。但由于查询命令是异步的,因此ui不会更新。这就是我在异步接收数据之前设置Listbox.ItemSource的原因。(我知道数据是通过断点接收的。(我知道我的绑定是正确的,我已经设置了Listbox.Itemsource,单击按钮后,数据显示在我的ui中) 我知道这可能与NOTIFY更改有关,所以我尝试了实现它。但我无法让它工作。所以基本上我的问题是: 如何使用表示为MobileSericCollection的异步数据源更新ui中的列表框 以下是我到目前为止的情况: Xaml: 视图模型:C# 绑定MobileServiceCollection未在UI(MVVM)中更新,c#,mvvm,windows-phone-8,binding,azure-mobile-services,C#,Mvvm,Windows Phone 8,Binding,Azure Mobile Services,我的视图模型中有一个移动服务集合,它从云中的移动服务数据库获取数据。我正在尝试将此MobileServiceCollection绑定到ui中的列表框(带有项目模板)。但由于查询命令是异步的,因此ui不会更新。这就是我在异步接收数据之前设置Listbox.ItemSource的原因。(我知道数据是通过断点接收的。(我知道我的绑定是正确的,我已经设置了Listbox.Itemsource,单击按钮后,数据显示在我的ui中) 我知道这可能与NOTIFY更改有关,所以我尝试了实现它。但我无法让它工作。所
class ProductListViewModel
{
public MobileServiceCollection<Product, Product> P;
public ProductListViewModel()
{
getProducts();
}
/// <summary>
/// Fetch all products from the database (async).
/// </summary>
private async void getProducts()
{
P = await App.MobileService.GetTable<Product>()
//.Where(Product => Product.ProductName == "tomato")
.OrderByDescending(Product => Product.ProductPrice)
.ToCollectionAsync();
}
}
如果有人能在这件事上帮助我,我将不胜感激。试着这样做: 首先,使您的
ProductListViewModel
实现INotifyPropertyChanged
Second,将p
设为属性,而不是字段/成员,因为数据绑定仅适用于公共属性。并正确发出属性更改通知:
private MobileServiceCollection<Product, Product> _p;
public MobileServiceCollection<Product, Product> P
{
get { return _p; }
set
{
_p = value;
NotifyPropertyChanged("P");
}
}
Forth,将ItemsSource
绑定路径设置为DataContext/ProductListViewModel的p
属性:
<ListBox x:Name="ListOfProducts" ItemsSource="{Binding P}">
[DataContract]
public class Product
{
[DataMember(Name = "id")]
public string id { get; set; }
[DataMember(Name = "ProductName")]
public string ProductName { get; set; }
[DataMember(Name = "ProductPrice")]
public float ProductPrice { get; set; }
}
private MobileServiceCollection<Product, Product> _p;
public MobileServiceCollection<Product, Product> P
{
get { return _p; }
set
{
_p = value;
NotifyPropertyChanged("P");
}
}
ProductListViewModel plvm = new ProductListViewModel();
public ProductList()
{
InitializeComponent();
this.DataContext = plvm;
}
<ListBox x:Name="ListOfProducts" ItemsSource="{Binding P}">