How can I navigate from an "external" .cs file in Silverlight
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开发者_StackOverflow社区.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
You can't use the function NavigationService.Navigate because NavigationService is a class here, not an object. I think you have to add NavigationService to the parameters in your constructor for home, which you can use then. Like this:
public void nav(NavigationService ANavigationService)
{
ANavigationService.Navigate(new Uri(""));
}
MainPage should have an instance of NavigationService, which you can pass to Home
Edit: You can get the an instance of the navigationservice in MainPage with this:
//in mainpage
NavigationService navService = NavigationService.GetNavigationService(this);
精彩评论