0

我已经坐在这个问题上几个小时了,我有这个部分 xaml 代码:

<TextBlock Text="{Binding temprature}" Height="30" HorizontalAlignment="Left" Margin="13,119,0,0" Name="textBlock1" VerticalAlignment="Top" Width="68" />
            <TextBlock Height="30" HorizontalAlignment="Left" Name="commentsTextBlock" Text="Comments:" VerticalAlignment="Bottom" Margin="12,0,0,-31" />
            <ListBox Margin="2,785,-14,-33" ItemsSource="{Binding comments}" DataContext="{Binding}" Name="commentsListBox">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                            <StackPanel Width="311">
                                <TextBlock Text="{Binding poster_username}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" TextTrimming="WordEllipsis" Width="Auto" Foreground="White" FontFamily="Segoe WP Semibold" />
                                <TextBlock Text="{Binding comment_text}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" TextTrimming="WordEllipsis" MaxHeight="100" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我有这个类(线程),其中包括一个应该显示在 ListBox 中的评论列表。

    public class Thread : INotifyPropertyChanged
{
    public string title { get; set; }
    public string deal_link { get; set; }
    public string mobile_deal_link { get; set; }
    public string deal_image { get; set; }
    public string deal_image_highres { get; set; }
    public string description { get; set; }
    public string submit_time { get; set; }
    public bool hot_date { get; set; }
    public string poster_name { get; set; }
    public double temperature { get; set; }
    public double price { get; set; }
    public int timestamp { get; set; }
    public string expired { get; set; }
    public Forum forum { get; set; }
    public Category category { get; set; }
    public object merchant { get; set; }
    public object tags { get; set; }
    public int thread_id { get; set; }
    public string visit_link { get; set; }
    public string hot_time { get; set; }
    public int comment_count { get; set; }
    public string availability { get; set; }
    public string can_vote { get; set; }
    public string seen { get; set; }

    public List<Comment> comments { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;

    public void Convert2Unicode()
    {
        UnicodeEncoding unicode = new UnicodeEncoding();
        Byte[] encodedBytes = unicode.GetBytes(title);
        title = String.Format("[{0}]", title);


    }


    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public void SetComments(string content)
    {
        PaginatedComments commentsList;
        this.comments.Clear();

        DataContractJsonSerializer serializer =
        new DataContractJsonSerializer(typeof(PaginatedComments));
        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
        {
            commentsList = (PaginatedComments)serializer.ReadObject(ms);
        }


        foreach (var thread in commentsList.data.comments)
        {
            this.comments.Add(thread);
        }

    }
    public Thread()
    {
        comments = new List<Comment>();

    }
    public void addComments()
    {
        List<string> parameters = new List<string>();
        parameters.Add("thread_id=" + thread_id);
        parameters.Add("results_per_page=10");
        parameters.Add("page=1");
        // Set the data context of the listbox control to the sample data
        APICalls.makeRequest(APICalls.ViewingPaginatedComments, parameters, SetComments);

    }
    //
    // Sets the "Seen" variable depending if the item is new (since the last time the application was opened
    //
    public void updateNewItems()
    {
        try
        {
            int last_seen = (int)IsolatedStorageSettings.ApplicationSettings["lastrun"];

            if (last_seen < timestamp)
            {
                seen = "New";
            }
            else
            {
                seen = "";
            }
        }
        catch (System.Exception e)
        {
            IsolatedStorageSettings.ApplicationSettings["lastrun"] = APICalls.GetIntTimestamp();
            IsolatedStorageSettings.ApplicationSettings.Save();
            MessageBox.Show(e.Message);
        }

        IsolatedStorageSettings.ApplicationSettings["lastrun"] = APICalls.GetIntTimestamp();
        IsolatedStorageSettings.ApplicationSettings.Save();

    }
}
public class Comment
{
    public string poster_username { get; set; }
    public string post_date { get; set; }
    public string comment_text { get; set; }
    public int timestamp { get; set; }
    public int like_count { get; set; }
    public string comment_edit_text { get; set; }
}

public class CommentData
{
    public List<Comment> comments { get; set; }
    public int comment_count { get; set; }
    public int total_comments { get; set; }
}

public class PaginatedComments
{
    public string response_status { get; set; }
    public CommentData data { get; set; }
}

如果我在将 DataCotext 更改为此特定线程之前将评论加载到线程中。显示了评论,但是当我在更改 DataContext 并导航到页面后更新评论时,没有显示评论(我在 xaml 页面的其余部分中还有其他字段绑定到同一个实例并且它们工作得很好。只有评论不起作用!

我真的很感谢你的帮助!谢谢

4

2 回答 2

2

你应该使用

public ObservableCollection<Comment> comments{ get; set;}

代替

public List<Comment> comments { get; set; } 

每当添加或删除其中一项(在本例中为 Comment )时,ObservableCollection 都会向视图发送更新通知。

注意:它不会更新 Comment 。要将项目绑定到 Comment 更新,Comment 必须实现 INotifyPropertyChanged。

于 2012-07-02T19:52:28.543 回答
2

您的属性是一个简单的List<T>. 当某些事情发生变化时,需要使用事件向 Silverlight 发出信号。

AList<T>在添加/删除项目时不会引发任何事件,因此 Silverlight 无法检测到新项目,因此不会更新 UI。完成这项工作的最简单方法通常是使用 anObservableCollection<T>而不是列表。此集合将引发 Silverlight 知道要侦听的事件。

请注意,要使其正常工作,您不应从 U/I(调度程序)线程以外的任何其他线程调用 Add/Remove/Clear,因为 Silverlight 控件不是线程安全的(并且事件在执行添加/删除/清除呼叫)。为此,只需确保从 SetComments 方法调用Dispatcher.BeginInvoke(因为无论您的获取机制是什么,它似乎都是一个回调)。

或者,您也可以在获取评论时重新生成一个全新的 List 对象,并在 SetComments 方法中引发 NotifyPropertyCHanged 事件,但这会导致丢失当前选择并将列表重置为顶部,这可能不是您想要的做。

于 2012-07-02T19:58:01.487 回答