Reputation: 227
I have 4 classes
Comment Class:
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;
}
}
Pin Class:
public class Pin : PhoneApplicationPage
public List<comment> Comment_List;
public Pin(){
this.Comment_List = new List<comment>();
}
}
Message Page:
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
}
The static function then updates a static class called PinList().
I have an event triggered in PinList() class when Its static List of Pins is updated, How to i address the object that is the current MessagePage to to call a function to update the textbox with the new values in Pin.comments.
i.e. i Have:
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();
}
Upvotes: 0
Views: 457
Reputation: 13970
From the code you've got and the way you've worded it it sounds like you're trying to access MessagePage
statically as if it was a singleton but there's not a single static Property anything there. If you're dead-set on doing it that way you'll need to declare a static instance of MessagePage
.
I would recommend, however, that you simply bind an ItemsControl
of some sort to react to the Pin.Comment_List
and make Comment_List an ObservableCollection<comment>
instead of a List<comment>
and make sure the comment class implements INotifyPropertyChanged
- then the UI will take care of its own updates. It looks like what you're doing is trying to reinvent the wheel.
Edit: Based on your comments
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));
}
}
}
Pin Class:
public class Pin
{
private readonly ObservableCollection<Comment> commentList = new ObservableCollection<Comment>();
public ObservableCollection<Comment> CommentList
{
get
{
return commentList;
}
}
}
Message Page:
public partial class MessagePage : PhoneApplicationPage
{
private readonly Pin pin;
public MessagePage(Pin _pin)
{
this.pin = _pin;
}
public Pin Pin
{
get
{
return pin;
}
}
And your data source
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
}
}
Displaying a Pin's comments would look something like this as long as you implemented INotifyPropertyChanged
on comment and changed Comment_List to an ObservableCollection<comment>
. All you would need to do in order to update the messages is add any new messages to that pin's Comments_List and the UI will react without you having to do anything in singleton or subscribe to a bunch of events.
<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>
Upvotes: 2