WP7将列表框绑定到WCF结果 [英] WP7 Binding Listbox to WCF result

查看:50
本文介绍了WP7将列表框绑定到WCF结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF调用,该调用返回一个对象列表。

I have a WCF call that returns a list of objects.

我创建了WP7 Silverlight Pivot应用程序,并修改了MainViewModel以从WCF服务中加载数据。 ,LoadData方法现在看起来像这样

I've created a WP7 Silverlight Pivot application and modified the MainViewModel to load data from my WCF service, the LoadData method now looks like this

public ObservableCollection<Standing> Items { get; private set; }

public void LoadData()
{
    var c = new WS.WSClient();
    c.GetStandingsCompleted += GetStandingsCompleted;
    c.GetStandingsAsync();            
}

void GetStandingsCompleted(object sender, GetStandingsCompletedEventArgs e)
{
    Items = e.Result;
    this.IsDataLoaded = true;
}

这会运行,如果我在已完成的事件上设置了断点,则可以看到它运行成功,我的项目集合现在有50个奇数项目。但是,UI中的列表框不会显示这些。

This runs and if I put a break point on the completed event I can see it ran successfully and my Items collection now has 50 odd items. However the listbox in the UI doesnt display these.

如果我将以下行添加到LoadData方法的底部,那么我会在UI的listbx中看到1个项目

If I add the following line to the bottom of my LoadData method then I see 1 item appear in the listbx on the UI

Items.Add(new Standing(){Team="Test"});

这证明了绑定是正确的,但是似乎由于Asynch WCF调用UI的延迟不会更新。

This proves the bindings are correct but it seems that because of the delay in the Asynch WCF call the UI doesn't update.

作为参考,我更新了MainPage.xaml列表框以绑定到我的站立对象上的Team属性。

For reference I've updates the MainPage.xaml listbox to bind to the Team property on my Standing object

<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel Margin="0,0,0,17" Width="432">
                <TextBlock Text="{Binding Team}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Text="{Binding Team}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
          </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

任何关于我做错事情的想法吗?

Any ideas on what I'm doing wrong?

谢谢

推荐答案

在第一次创建 ListBox 时, ItemsSource 属性采用 Items 的当前值,即 null 。当WCF调用完成并为 Items 分配新值时,如果不触发<$ c $,则视图无法知道该属性的值已更改。 c> PropertyChanged 事件,正如Greg Zimmers在回答中提到的那样。

When your ListBox is first created its ItemsSource property takes the current value of Items which is null. When your WCF call completes and you assign a new value to Items, the view has no way of knowing that the value of that property has changed without you firing a PropertyChanged event, as Greg Zimmers mentions in his answer.

由于您使用的是 ObservableCollection ,因此另一种方法是首先创建一个空集合,然后添加

Since you're using an ObservableCollection, an alternative method would be to initially create an empty collection and then add objects to it when the WCF call completes.

private ObservableCollection<Standing> _items = new ObservableCollection<Standing>();
public ObservableCollection<Standing> Items
{ 
  get { return _items; } 
  private set;
}


void GetStandingsCompleted(object sender, GetStandingsCompletedEventArgs e)
{
    foreach( var item in e.Result ) Items.Add( item );
    this.IsDataLoaded = true;
}

这篇关于WP7将列表框绑定到WCF结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆