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_ListComment_ListObservableCollection<comment>を a ではなく にし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>. メッセージを更新するために必要なことは、そのピンの 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 に答える