I know the title isn't very clear, I'll try to explain myself better.
I've got in my MainPage.xaml a Listbox that I want to populate in a certain way. On that purpose, I have a Home.cs file with no xaml attached, whose constructor is like this:
public Home(string sid, ListBox lb){...}
In this constructor I do several POST requests with Webclient and, in the UploadStringCompleted method, I create several elements, stackpanels, textblocks and a hyperlinkbutton.
StackPanel bigPanel = new StackPanel();
bigPanel.Width = 456;
HyperlinkButton hyperlink = new HyperlinkButton();
hyperlink.Content = friend.name + " " + person.surname;
hyperlink.Click += new RoutedEventHandler(hyperlink_Click);
bigPanel.Children.Add(hyperlink);
lb.Items.Add(bigPanel);
And here comes the problem. I want this hyperlink to navigate to another page (FriendPage.xaml), but I can't achieve it, because if I try to add NavigationService.Navigate(new Uri("/FriendPage.xaml", Urikind.Relative)) then it fails, it says "An object reference is required for the non-static method Navigate".
As far as I know, this code line should be in MainPage.xaml, so, is there a way to do this? Or to place the hyperlink.Click event handler in MainPage.xaml.cs and bind it to a hyperlinkbutton that does not exist yet?
Thank you