【问题标题】:Binding MobileServiceCollection not updating in UI (MVVM)绑定 MobileServiceCollection 未在 UI (MVVM) 中更新
【发布时间】: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


    【解决方案1】:

    尝试这样做:

    首先,让你的ProductListViewModel实现INotifyPropertyChanged

    第二,将P 设为属性而不是字段/成员,因为数据绑定仅适用于公共属性。并正确提出属性更改通知:

    private MobileServiceCollection<Product, Product> _p;
    public MobileServiceCollection<Product, Product> P
    {
        get { return _p; }
        set 
        {
            _p = value;
            NotifyPropertyChanged("P");
        }
    }
    

    第三,正确设置视图的DataContext。相对于视图当前 DataContext 的数据绑定解析路径:

    ProductListViewModel plvm = new ProductListViewModel();
    public ProductList()
    {
        InitializeComponent();
        this.DataContext = plvm;
    }
    

    Forth,将ItemsSource绑定路径设置为DataContext/ProductListViewModel的P属性:

    <ListBox x:Name="ListOfProducts" ItemsSource="{Binding P}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多