If I do this inside a User Control:
NavigationService.Navigate(new Uri("/Alliance.xaml", UriKind.Relative));
it says this error:
An object reference is required for the non-static field, metho开发者_Go百科d, or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)'
Thank you
Well, I solved passing the normal Page as an argument to the User Control, so I could get the NavigationService.
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(uri);
I normally use an EventHandler. Example: in your user control, define something like
public event EventHandler goToThatPage;
that you will call in your control foe example like this:
goToThatPage(this, new EventArgs());
Then in the constructor of your MainPage.xaml.cs (if the user control is contained there) you will define:
uxControlName.goToThatPage+= new EventHandler(ControlGoToThatPage);
and somewhere in your MainPage.xaml.cs you finaly declare the action to be done:
void ControlGoToThatPage(object sender, EventArgs e)
{
this.NavigationService.Navigate(new Uri("/Pages/ThatPage.xaml", UriKind.Relative));
}
Here is another solution for Silverlight for Windows Phone 8:
public Page Page { get; set; }
this.Loaded += delegate
{
Page = (Application.Current.RootVisual as Frame).Content as Page;
};
Page.NavigationService.Navigate(new Uri("/Alliance.xaml", UriKind.Relative));
NavigationService is a class. Navigate is a method you can call on instances of that class. It is not a static method you can call from outside an object reference.
Basically you need to get the current NavigationService for the current page. This link http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.aspx should help.
NavigationService is a property of the page object in Silverlight, which is why you are getting this error. It is not a property of a UserControl in Silverlight.
The following are a few options which will be able to solve the issue you're seeing.
Treat the usercontrol as a control. Give it an event which it will fire when the button is clicked. The page can listen for that event and handle the navigation when it fires.
You can either allow your page access to its parent or pass the NavigationService from the page to the usercontrol.
You can also set this up using messaging, but that would be more complicated.Many MVVM frameworks have messaging features. MVVM Light has it.
((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame"))
.Navigate(new Uri("Page Name", UriKind.Relative));
I know this is old, but I was also in the same situation on a Silverlight app. I wanted to do something similar to Deepak's answer, but I couldn't figure it out for the longest time why it didn't work in my case.
Turns out I needed to call refresh, and not navigate; because I originally thought that navigate would reload the page if the URI is the same as the current page. Forgive my beginner-ness.
(((Application.Current.RootVisual as MainPage).ContentFrame as Frame).Content as Page).NavigationService.Refresh();
if(Application.Current.RootVisual is Page)
{
((Page) (Application.Current.RootVisual)).NavigationService.Navigate(uri);
}
精彩评论