在wp中,使用NavigationService.Navigate导航页面出现错误
我們在WP項目中采用頁面導航時候,經(jīng)常會使用以下代碼
NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relative));但是,有的時候會出現(xiàn)錯誤: "Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)'
我了解了一下,
http://stackoverflow.com/questions/6003212/wp7-navigationservice-navigate-is-complaining-that-it-is-not-receiving-an-obj
從上面這個答案中可以了解到:
The Navigate method is actually part of the non-static NavigationService class.
Since it's non-static, you need to create an instance of it. The reason you haven't had to create an instance before is because it's part of the Page object,?
but since you're not inheriting from the Page object, you don't have access to the NavigationService instance.
There are various ways around this such as creating an event handler in your usercontrol that your host Page object (e.g. MainPage) can subscribe to and have it fire the NavigationService on its behalf.
Or you can simply access the NavigationService from the Application host like so:
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(targetUri);
轉載于:https://www.cnblogs.com/fifa0329/p/4536655.html
總結
以上是生活随笔為你收集整理的在wp中,使用NavigationService.Navigate导航页面出现错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用apache的httpclient发请
- 下一篇: UVAlive 6131 dp+斜率优化