Silverlight - How to navigate from a User Control

2019-01-18 04:03发布

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, method, 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.

8条回答
Ridiculous、
2楼-- · 2019-01-18 04:22

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();
查看更多
狗以群分
3楼-- · 2019-01-18 04:25

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.

查看更多
登录 后发表回答