0

我有 4 节课

评论类:

public class comment
    {
        public string Username { get; set; }
        public string Comment { get; set; }

        public comment(string _username, string _comment)
        {
            this.Username = _username;
            this.Comment = _comment;
        } 
    }

针类:

   public class Pin : PhoneApplicationPage

        public List<comment> Comment_List;        

    public Pin(){

        this.Comment_List = new List<comment>();
    }
}

留言页面:

public partial class MessagePage : PhoneApplicationPage
{
   Pin _Pin;

   public MessagePage(Pin _pin)
 {        
    this._Pin = _pin;
 }

 public void Refresh()
 {
   this.textbox1.Text = "";
   foreach (comment c in this._Pin.List)
   {
    this.textbox1.Text += c.Username;
   }

  }

public void function()
{
     //Call static function in another class to download new pin info
}

然后静态函数更新一个名为 PinList() 的静态类。

当其静态引脚列表更新时,我在 PinList() 类中触发了一个事件,如何解决当前 MessagePage 的对象以调用函数以使用 Pin.comments 中的新值更新文本框。

即我有:

public class PinList
    {
        public ObservableCollection<Pin> list;
        public static ObservableCollection<Pin> MainPinList = new ObservableCollection<Pin>();
        public event PropertyChangingEventHandler PropertyChanged;

public PinList()
        {
            list = new ObservableCollection<Pin>();
            list.CollectionChanged += listChanged;            

            ((INotifyPropertyChanged)list).PropertyChanged += new PropertyChangedEventHandler(list_Property_Changed);

        }


    private void list_Property_Changed(object sender, PropertyChangedEventArgs args)
            {
                  //Need to call
                  //MessagePage.Refresh();
            }
4

1 回答 1

2

从您获得的代码和您的措辞方式来看,听起来您正在尝试MessagePage静态访问,就好像它是一个单例一样,但那里没有一个静态属性。如果你死心塌地这样做,你需要声明一个MessagePage.

但是,我建议您简单地绑定ItemsControl某种类型的Pin.Comment_Listan以对 看起来你正在做的是试图重新发明轮子。ObservableCollection<comment>List<comment>INotifyPropertyChanged

编辑:根据您的评论

public class Comment : INotifyPropertyChanged
{
    private string username;
    private string comment;

    public comment(string _username, string _comment)
    {
        this.Username = _username;
        this.Comment = _comment;
    }                 

    public string Username
    {
        get
        {
            return username;
        }

        set
        {
            if(value != username)
            {
                username = value;
                NotifyPropertyChanged("Username");
            }
        }
    }                   

    public string Comment
    {
        get
        {
            return comment;
        }

        set
        {
            if(value != comment)
            {
                comment= value;
                NotifyPropertyChanged("Comment");
            }
        }
    }       

    public event PropertyChangedEventHandler PropertyChanged;

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

针类:

public class Pin
{
    private readonly ObservableCollection<Comment> commentList = new ObservableCollection<Comment>();        

    public ObservableCollection<Comment> CommentList
    {
        get
        {
            return commentList;
        }
    }
}

留言页面:

public partial class MessagePage : PhoneApplicationPage
{
    private readonly Pin pin;

    public MessagePage(Pin _pin)
    {        
        this.pin = _pin;
    }

    public Pin Pin
    {
        get
        {
            return pin;
        }
    }

还有你的数据源

public class PinList
{
    public ObservableCollection<Pin> list;
    public static ObservableCollection<Pin> MainPinList = new ObservableCollection<Pin>();

    public void Refresh()
    {
         // Here you update the comments list of each of your Pins - The comment
         //     list is an ObservableCollection so your display will automatically
         //     update itself. If you have to change an existing comment due to
         //     an edit or something that will automatically update as well since
         //     we've implemented INotifyPropertyChanged
    }
}

只要您INotifyPropertyChanged在评论上实现并将 Comment_List 更改为ObservableCollection<comment>. 为了更新消息,您需要做的就是将任何新消息添加到该 pin 的 Comments_List 中,UI 将做出反应,而无需您在单例中执行任何操作或订阅一堆事件。

<ItemsControl DataContext={Binding Pin} ItemsSource="{Binding CommentList}"">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <StackPanel Margin="0 0 0 10">
        <TextBlock Text="{Binding Username, StringFormat='{0} said:'}" FontWeight="Bold" />
        <TextBlock Text="{Binding Comment}" />
      </StackPanel>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>
于 2012-12-04T23:26:36.007 回答