从App.xaml.cs导航 [英] Navigating from App.xaml.cs

查看:250
本文介绍了从App.xaml.cs导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个应用程序栏添加到我的应用程序的多个页面。所以,我定义应用程序栏作为应用程序资源,以便它可以由多个页面中使用。现在,这些按钮的事件处理程序作为这里所说的应用类的 http://msdn.microsoft.com/en-us/library/hh394043%28v=VS.92%29.aspx
但是,这些应用程序栏按钮基本上都是快捷方式重要的网页。因此,点击一个按钮只会带你到相应的页面。但是,因为我在定义 App.xaml.cs 事件处理程序,它不允许我导航。我理解这样做的原因。但是,我不知道如何解决这个问题。

I want to add an application bar to multiple pages of my app. So, I'm defining the application bar as an application resource so that it can be used by multiple pages. Now, the event handlers for these buttons are in the App class as mentioned here http://msdn.microsoft.com/en-us/library/hh394043%28v=VS.92%29.aspx. But, these app bar buttons are basically shortcuts to important pages. So, clicking a button would just take you to the corresponding page. But, since I'm defining the event handlers in App.xaml.cs, it doesn't allow me to navigate. I understand the reason for this. But, I don't know how to solve the problem.

NavigationService.Navigate(new Uri("/Counting.xaml", UriKind.RelativeOrAbsolute));



说一个对象引用是所必需的非静态字段,方法或属性System.Windows。 Navigation.NavigationService.Navigate(的System.Uri)

says "An object reference is required for the non-static field, method or property System.Windows.Navigation.NavigationService.Navigate(System.Uri)"

推荐答案

请问如果你访问框架工作?

Does it work if you get access to the frame?

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Counting.xaml", UriKind.RelativeOrAbsolute));



编辑:
每个应用程序只有一个的框架。正是这种帧公开的NavigationService 。因此,的NavigationService总是通过框架访问,因为总有在任何Windows Phone应用程序的它的实例。既然你通常不会实例化一个新的的NavigationService ,人们很容易认为这是一个静态方法。但是,它实际上是运行你的应用程序时就会自动实例化的非静态类。所有你在这种情况下,这样做是越来越全局实例,其被连接到总是存在的帧,以及使用的网页之间进行导航。这意味着你的类没有实例,或明确继承,一个的NavigationService。

Each application has only one Frame. It's this frame that exposes the NavigationService. Therefore, the NavigationService is always accessible via the frame since there's always an instance of it in any Windows Phone app. Since you don't usually instantiate a new NavigationService, it's easy to think that it's a static method. However, it's actually a non-static class that gets instantiated automatically when your app is run. All you're doing in this case is getting the global instance, which is attached to the always-present Frame, and using that to navigate between pages. This means your class does not have to instantiate, or explicitly inherit, a NavigationService.

这篇关于从App.xaml.cs导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆