【问题标题】:What's wrong with my Windows Phone 7 Databinding(no viewmodels used)?我的 Windows Phone 7 数据绑定有什么问题(未使用视图模型)?
【发布时间】:2012-11-28 07:22:08
【问题描述】:

我在数据绑定方面遇到了困难。我可以成功获得结果,但它不会显示。这是我的代码:

private List<FacebookFriend> friendList;    
public List<FacebookFriend> FriendList
        {
            get { return friendList; }
            set
            {
                friendList = value;
                NotifyPropertyChanged("FriendList");
            }
        }
private void GetFbFriends()
    {
        var fb = new FacebookClient(_accessToken);
        friendList = new List<FacebookFriend>();
        fb.GetCompleted += (o, e) =>
        {
            if (e.Error != null)
            {
                return;
            }
            var result = (JsonObject)e.GetResultData();

            foreach (var friend in (JsonArray)result["data"])
                friendList.Add(new FacebookFriend()
            {
                Id = (string)(((JsonObject)friend)["id"]),
                Name = (string)(((JsonObject)friend)["name"])
            });
            FriendList = friendList;
        };
        fb.GetAsync("me/friends");
    }

然后在页面的xaml中:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding FriendList}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <StackPanel Background="Red" Height="100" Width="300" Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=Name}"/>
                                <TextBlock Text="{Binding Path=Id}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

这似乎是正确的,但仍然没有显示任何内容。任何帮助表示赞赏。非常感谢!

【问题讨论】:

    标签: data-binding windows-phone-7.1


    【解决方案1】:

    尝试使用ObservableCollection&lt;&gt; 而不是list&lt;&gt;。更多信息请查看this

    注意:ObservableCollection 是一个通用动态数据集合,当items get added, removed, or when the whole collection is refreshed 时提供通知(使用接口“INotifyCollectionChanged”)。

    【讨论】:

    • 我尝试过您的建议,但仍然没有显示任何内容。我添加了这个公共部分类 FacebookInfoPage :PhoneApplicationPage, INotifyPropertyChanged
    • 您确定列表/集合不为空吗?
    • 在 page.xaml 中做 this.DataContext = this; 在构造函数中,我希望您上面的代码在 page.xaml 本身中。这应该这样做。还要在运行时检查Output 窗口是否存在绑定错误。
    • 谢谢! this.DataContext = 这丢失了。 :)
    • 很高兴为您提供帮助。如果您觉得合适,请接受答案;)
    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多