C# - 从不同的类调用函数

时间:2012-12-04 23:03:59

标签: c# silverlight windows-phone-7 events static

我有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()类的静态列表更新时,我在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();
            }

1 个答案:

答案 0 :(得分:2)

从您获得的代码和您的措辞方式来看,听起来您正试图静态访问MessagePage,就好像它是一个单身人士,但那里没有任何一个静态属性。如果你这样做就死定了,你需要声明一个MessagePage的静态实例。

但是,我建议您只需绑定某种ItemsControl以对Pin.Comment_List作出反应,并使Comment_List成为ObservableCollection<comment>而不是List<comment>并制作确保评论类实现INotifyPropertyChanged - 然后UI将处理自己的更新。看起来你正在做的是试图重新发明轮子。

修改:根据您的评论

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>