【发布时间】:2014-04-23 18:11:17
【问题描述】:
我的视图模型中有一个移动服务集合,它从云中的移动服务数据库获取数据。我正在尝试将此 MobileServiceCollection 绑定到我的 ui 中的列表框(带有项目模板)。但是由于 Query 命令是异步的,所以 ui 不会更新。这是我在异步接收数据之前设置 Listbox.ItemSource。 (我知道数据是通过断点接收的。(我知道我的绑定是正确的,我在单击按钮后设置了 Listbox.Itemsource 然后数据显示在我的 ui 中)。
我知道这可能与 NotifyChange 有关,所以我尝试实现它。但我无法让它工作。所以基本上我的问题是:
如何使用表示为 MobileSericeCollection 的异步数据源更新我的 UI 中的列表框。
这是我目前所拥有的:
Xaml:
<ListBox x:Name="ListOfProducts" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock x:Name="tb1" Margin="0,0,0,0" Text="{Binding ProductName, Mode=TwoWay}"></TextBlock>
<TextBlock x:Name="tb2" Margin="200,0,0,0" Text="{Binding ProductPrice, Mode=TwoWay}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后面的代码:
public partial class ProductList : PhoneApplicationPage
{
ProductListViewModel plvm = new ProductListViewModel();
public ProductList()
{
InitializeComponent();
ListOfProducts.ItemsSource = plvm.P;
}
}
视图模型:
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();
}
}
型号:
[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; }
}
如果有人能帮我解决这个问题,那就太感谢了
【问题讨论】:
标签: c# mvvm windows-phone-8 binding azure-mobile-services